Sunday, April 2, 2023

How to fix access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set? [Solved]

Hello and welcome to the blog post. Today we are going to take a look at another frequently encountered issue while working with Spring Boot and Hibernate. The error "Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set" usually comes when Hibernate is unable to determine the appropriate database dialect to use. This error can be fixed by setting the hibernate.dialect property in your Hibernate configuration file. In this post, we'll look at a fix for the issue where a Spring Boot applications' "hibernate.dialect" setting prevents access to dialectresolutioninfo from being null., But before that let's take a look at the Exception itself.

Below is the stack trace of the error

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl .determineDialect(DialectFactoryImpl.java:104) at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl .buildDialect(DialectFactoryImpl.java:71) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl .configure(JdbcServicesImpl.java:205) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl .configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl .initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl .getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4 .perform(EntityManagerFactoryBuilderImpl.java:850) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4 .perform(EntityManagerFactoryBuilderImpl.java:843) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl .withTccl(ClassLoaderServiceImpl.java:398) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl .build(EntityManagerFactoryBuilderImpl.java:842) at org.hibernate.jpa.HibernatePersistenceProvider .createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean .createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean .afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory .invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory .initializeBean(AbstractAutowireCapableBeanFactory.java:1550) ... 21 more



 How to fix access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set?

According to this exception, hibernate.dialect is empty. The main issue with this error is that it doesn't specifically state why this exception is occurring. Although it first appears that we omitted dialect information, we might still receive an exception even when we gave the correct dialect. We may obtain this exception if we submitted the wrong password or by accident provided the wrong database name or because of any other reason. 

To fix the error, you can add the following line to your Hibernate configuration file, where MyDialect is the appropriate dialect for your database:
hibernate.dialect=org.hibernate.dialect.MyDialect


If you're using Spring Boot, you can also set the spring.jpa.properties.hibernate.dialect property in your application.properties or application.yml file, like this:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MyDialect

Once you have set the appropriate dialect for your database, the error should be resolved and Hibernate should be able to connect to your database successfully.

Now, that you know how to solve this error, let's see a live example project where we will first see how and when this error comes and then we will see different ways to fix this error.

How to fix access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set? [Solved]



Following are the files residing in our simple project.

Pom.xml

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>




<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>




<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.32</version>

</dependency>




<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

<version>3.0.4</version>

</dependency>




<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

</dependencies>


Application.properties

spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dbName?useSSL=false

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect


SpringBootMainApplication.java


package com.practice.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication()

public class SpringBootPracticeApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringBootPracticeApplication.class, args);
   }
}


HibernateConfig.java

@Configuration

@EnableTransactionManagement

@ComponentScan({ "com.spring.app" })

public class HibernateConfig {

   @Bean

   public LocalSessionFactoryBean sessionFactory() {

      LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

      sessionFactory.setDataSource(restDataSource());

      sessionFactory.setPackagesToScan(new String[] { "com.practice.springboot.entity" });

      sessionFactory.setHibernateProperties(hibernateProperties());

      return sessionFactory;

   }

   @Bean

   public DataSource restDataSource() {

      BasicDataSource dataSource = new BasicDataSource();

      dataSource.setDriverClassName("com.mysql.jdbc.Driver");

      dataSource.setUrl("jdbc:postgresql://localhost:3306/dbName?useSSL=false");

      dataSource.setUsername("user");

      dataSource.setPassword("password");

      return dataSource;

   }

   @Bean

   @Autowired

   public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {

      HibernateTransactionManager txManager = new HibernateTransactionManager();

      txManager.setSessionFactory(sessionFactory);

      return txManager;

   }

   @Bean

   public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {

      return new PersistenceExceptionTranslationPostProcessor();

   }

   Properties hibernateProperties() {

      return new Properties() {

        private static final long serialVersionUID = 1L;

        {

            setProperty("hibernate.hbm2ddl.auto", "create");

            setProperty("hibernate.show_sql", "false");

            setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

         }

      };

   }

}



Let’s take a look at how to fix this issue.

Ensure you have following properties in your application.properties file.

spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true

spring.datasource.username=root
spring.datasource.password=log500900500
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/funGun?useSSL=false

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect


In Spring Boot based java application adding the following missing line could solve the problem. 


spring.jpa.database-platform=org.hibernate.dialect

Just provide the appropriate value in the above line in accordance with the database that the Java-based application is utilising. Since I’m using MySQL database, the value of the'spring.jpa.database-platform' will be as follows. 

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect


Check for the dependencies 

Put the right database dependency in your pom.xml file.

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.32</version>
</dependency>


<dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <version>2.1.214</version>
   <scope>test</scope>
</dependency>


Configuration using SessionFactory 

One way you can configure the SessionFactory, by placing the below code in your application.

Configuration configuration = new Configuration();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
        .applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry)


Check the DB connection

Verify that the database server is operational. Typically, the Oracle, MySQL, and/or Postgresql servers are set up and operational at system setup. It's conceivable that these servers didn't start up correctly when the system first started. Ensure that your database server is operational. 


Set hibernate Dialect accordingly

If you have configured EntityManager manually adding the dialect should be like the one mentioned below.

entityManager.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");


Conclusion

We appreciate your patience as we examine in detail the issue DialectResolutionInfo cannot be null when 'hibernate.dialect' is not set. This article describes several ways to resolve the described problem, including the need for hibernate dialect, hibernate dependencies, checking for a database connection, and correctly configuring the session factory. You will be able to correct this problem after using the procedures suggested, We confident and are sincerely hope you find this post useful. 


No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.