Access to dialectresolutioninfo cannot be null when ‘hibernate.dialect’ not set

In this post, we will see how to fix Access to dialectresolutioninfo cannot be null when ‘hibernate.dialect’ not set in Spring Boot application. This exception says hibernate.dialect is null. The major problem with this error is it doesn’t say the exact reason why this exception is happening. At the first look, it seems we missed dialect information, but we may get this exception even we provided proper dialect. We may get this exception if we provided the wrong password or by mistake provided the wrong database name or because of some other reason.

Hibernate determines the correct dialect automatically but sometimes because of some wrong code or configuration hibernate is not able to figure out dialect. There might several reasons for “Access to dialectresolutioninfo cannot be null when ‘hibernate.dialect’ not set” error.

Note – Before going forward check the error for “Access denied” in the tomcat console or log. If you see this error we need to make changes accordingly.

Troubleshooting

1. Providing wrong Datasource information – If we provide the wrong URL, database password or wrong db name, we may get Access to dialectresolutioninfo that cannot be null when ‘hibernate.dialect’ is not set. Make sure the database is created and mentioned correctly in the application.properties file or context.xml. See example how to configure datasource using tomcat’s context.xml. Also, Don’t forget to check for URL, username, and password.

2. Make sure we are provinding correct dialect information in application.properties file

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
or
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
or
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

3. Check for proper MySql or Oracle dependency in pom.

    //MySql dependency  
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    //Oracle Dependency
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <scope>runtime</scope>
    </dependency>
    //Postgresql
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

4. If you are using persistence.xml then don’t forget to provide dialect name in persistent.xml

<property name="spring.jpa.database-platform" value="org.hibernate.dialect.PostgreSQLDialect" />

Or

<property name="spring.jpa.database-platform" value="org.hibernate.dialect.MySQL5Dialect"/>

Or

<property name="spring.jpa.database-platform" value="org.hibernate.dialect.Oracle10gDialect"/>

5. If you are using sessionFactory for configuration then add the below changes.

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

6. Check Database server up and running – Generally Oracle and MySql or Postgresql server is automatically up and running while system startup. It might possible these servers not getting up properly in system startup. Make sure our database server is up and running.

For example, the oracle server is up and running

Access to dialectresolutioninfo cannot be null when 'hibernate.dialect' not set

7. Use proper Datasource configuration

application.properties file MySQL database

spring.datasource.url=jdbc:mysql://localhost:3306/springbootcrudexample
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
server.port = 9091

application.properties file Oracle database

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.username=SYSTEM
spring.datasource.password=oracle
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql=true 
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
server.port = 9091

application.properties file for PostgreSQL database

spring.datasource.url=jdbc:postgresql://postgres/my_db
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

8. If you have configured EntityManager manually then add hibernate.dialect as below.

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

9. Try to add the below property in appliation.properties file

spring.jpa.database=mysql
Hope this helps! Still 

Let’s see the code that is responsible for Access to dialectresolutioninfo cannot be null when ‘hibernate.dialect’ not set exception.

Access to dialectresolutioninfo cannot be null when 'hibernate.dialect' not set

stacktrace for this exception

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.(InFlightMetadataCollectorImpl.java:176)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:118)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
at com.javatute.config.OracleDatasourceConfig.entityManagerFactory(OracleDatasourceConfig.java:55)
at com.javatute.config.OracleDatasourceConfig$$EnhancerBySpringCGLIB$$332cfb07.CGLIB$entityManagerFactory$1()
at com.javatute.config.OracleDatasourceConfig$$EnhancerBySpringCGLIB$$332cfb07$$FastClassBySpringCGLIB$$a29bf342.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at com.javatute.config.OracleDatasourceConfig$$EnhancerBySpringCGLIB$$332cfb07.entityManagerFactory()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
… 86 more

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:100)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)

That’s all about Access to dialectresolutioninfo cannot be null when ‘hibernate.dialect’ not set.

See docs.

JPA tutorials

Hibernate tutorials