[name=Redis, majorVersion=6]; user must register resolver or explicitly set 'hibernate.dialect'

80 views Asked by At

After successfully creating datasource for redis in wildfly, I am now trying to make connection to redis from my spring project. I am quite new to Spring, naturally I have stumbled upon some issues.

My pom.xml looks like this

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>MY_GRP_ID</groupId>
<artifactId>MAI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>NAME</name>
<description>DESC</description>
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <!--deploy.wildfly.host>XXX</deploy.wildfly.host-->
    <deploy.wildfly.host>XXX</deploy.wildfly.host>
    <deploy.wildfly.port>XXX</deploy.wildfly.port>
    <deploy.wildfly.username>XXX</deploy.wildfly.username>
    <deploy.wildfly.password>XXX</deploy.wildfly.password>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- logging framework -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    <!--dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </dependency-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>
    <!-- logging end -->

    <!-- GRAPH QL -->
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-spring-boot-starter</artifactId>
        <version>5.0.2</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java</artifactId>
        <version>18.0</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java-tools</artifactId>
        <version>5.2.4</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java-servlet</artifactId>
        <version>6.1.3</version>
    </dependency>
    <!-- GRAPH QL END-->
    <!--dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <version>2.7.2</version>
    </dependency-->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>2.3.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>5.0.2</version>
        <type>jar</type>
    </dependency>
</dependencies>

<build>
    <plugins>
        <!--plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin-->
        <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>2.0.2.Final</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <filename>${project.build.finalName}.war</filename>
                <hostname>${deploy.wildfly.host}</hostname>
                <port>${deploy.wildfly.port}</port>
                <username>${deploy.wildfly.username}</username>
                <password>${deploy.wildfly.password}</password>
            </configuration>
        </plugin>
    </plugins>
</build>

NOTE: This is a large project having multiple dependencies.

I have created a Student.java

import org.springframework.data.redis.core.RedisHash;

import java.io.Serializable;

@RedisHash("Student")
public class Student implements Serializable {
    private long id;
    private String name;
    //boilerplate constructor, setter and getter    
}

I have also created a StudentRepository.java

import org.springframework.data.repository.CrudRepository;
public interface StudentRepository extends CrudRepository<Student, Long> {}

So far so good. I built the war and deployed in wildfly. In server.log, I get the following output

2023-12-21 09:32:34,915 INFO  [org.springframework.data.repository.config.RepositoryConfigurationDelegate] (ServerService Thread Pool -- 393) Finished Spring Data repository scanning in 55 ms. Found 2 JPA repository interfaces.
2023-12-21 09:32:34,943 INFO  [org.springframework.data.repository.config.RepositoryConfigurationDelegate] (ServerService Thread Pool -- 393) Finished Spring Data repository scanning in 6 ms. Found 1 Redis repository interfaces.
2023-12-21 09:32:34,928 INFO  [org.springframework.data.repository.config.RepositoryConfigurationDelegate] (ServerService Thread Pool -- 393) Multiple Spring Data modules found, entering strict repository configuration mode!
2023-12-21 09:32:34,929 INFO  [org.springframework.data.repository.config.RepositoryConfigurationDelegate] (ServerService Thread Pool -- 393) Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2023-12-21 09:32:34,943 INFO  [org.springframework.data.repository.config.RepositoryConfigurationDelegate] (ServerService Thread Pool -- 393) Finished Spring Data repository scanning in 6 ms. Found 1 Redis repository interfaces.
2023-12-21 09:32:35,241 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 393) Initializing Spring embedded WebApplicationContext
2023-12-21 09:32:35,241 INFO  [org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext] (ServerService Thread Pool -- 393) Root WebApplicationContext: initialization completed in 875 ms
2023-12-21 09:32:35,488 INFO  [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 393) HHH000204: Processing PersistenceUnitInfo [name: default]
2023-12-21 09:32:35,529 INFO  [org.hibernate.Version] (ServerService Thread Pool -- 393) HHH000412: Hibernate ORM core version 5.6.9.Final
2023-12-21 09:32:35,667 INFO  [org.hibernate.annotations.common.Version] (ServerService Thread Pool -- 393) HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-12-21 09:32:35,772 WARN  [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] (ServerService Thread Pool -- 393) HHH000342: Could not obtain connection to query metadata: org.hibernate.HibernateException: Unable to determine Dialect to use [name=Redis, majorVersion=6]; user must register resolver or explicitly set 'hibernate.dialect'

Upon my search on internet on hibernate.dialect, I found that one needs to create hibernate.cfg.xml in resources directory with the content

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.ogm.datastore.redis.RedisHashDialect</property>
    </session-factory>
</hibernate-configuration>

But the dialect issue remains. I am not sure what to do now. Any help will be appreciated.

NOTE: Sorry for the long post, I have tried to be as detailed as possible.

EDIT I have added spring.jpa.properties.hibernate.dialect=org.hibernate.ogm.datastore.redis.RedisHashDialect in application.properties and added the following dependency in pom.xml

<dependency>
    <groupId>org.hibernate.ogm</groupId>
    <artifactId>hibernate-ogm-redis</artifactId>
    <version>5.1.0.Final</version>
</dependency>

Now I get the error

[org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] (ServerService Thread Pool -- 579) HHH000342: Could not obtain connection to query metadata: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Could not instantiate named strategy class [org.hibernate.ogm.datastore.redis.RedisHashDialect]
...
...
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.ogm.datastore.redis.RedisHashDialect]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:133)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:152)
... 63 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.ogm.datastore.redis.RedisHashDialect
at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210)

EDIT It seems like the RedisHashDialect class is not found. I have added the entry in application.properties file. Still it can not be found.

1

There are 1 answers

2
charlb On

Instead of hibernate-ogm-redis, rather stay in the Spring Boot world. One option is to use the Spring Boot Data Repository abstraction, using one of these dependencies.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

or

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
    </dependency>

See https://docs.spring.io/spring-data/redis/reference/index.html