Tutorial

[Solved] HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

Published on August 3, 2022
author

Pankaj

[Solved] HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

I was following Hibernate Official Documentation to create a basic hibernate application with version 4.3.5.Final. When I executed the application, I got hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set even though all the configuration seemed fine to me.

HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set

It took me long time to solve the problem and it turned out to be with the way SessionFactory instance was getting created. My utility class to create SessionFactory instance was like below.

package com.journaldev.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class SessionFactoryUtil {

	private static SessionFactory sessionFactory;
	
	private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
       	SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml")
        				.buildSessionFactory(new StandardServiceRegistryBuilder().build());
        	
            return sessionFactory;
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
	
	public static SessionFactory getSessionFactory() {
		if(sessionFactory == null) sessionFactory = buildSessionFactory();
        return sessionFactory;
    }
}

And I was getting following stack trace:

May 07, 2014 7:11:59 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
May 07, 2014 7:12:00 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: employee.hbm.xml
May 07, 2014 7:12:00 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
May 07, 2014 7:12:00 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Initial SessionFactory creation failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Exception in thread "main" java.lang.NullPointerException
	at com.journaldev.hibernate.main.HibernateMain.main(HibernateMain.java:38)

From the logs it’s clear that Configuration class was able to find the hibernate configuration file and it has hibernate.dialect defined, so the exception was not making any sense.

Fix for HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set

After some research and debugging, I was able to fix it. The part that is missing in the official documentation is that we need to apply the configuration properties to StandardServiceRegistryBuilder. When I changed the utility class to below, everything worked like a charm.

package com.journaldev.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class SessionFactoryUtil {

	private static SessionFactory sessionFactory;
	
	private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
        	Configuration configuration = new Configuration();
        	configuration.configure("hibernate.cfg.xml");
        	System.out.println("Hibernate Configuration loaded");
        	
        	//apply configuration property settings to StandardServiceRegistryBuilder
        	ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        	System.out.println("Hibernate serviceRegistry created");
        	
        	SessionFactory sessionFactory = configuration
        						.buildSessionFactory(serviceRegistry);
        	
            return sessionFactory;
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
	
	public static SessionFactory getSessionFactory() {
		if(sessionFactory == null) sessionFactory = buildSessionFactory();
        return sessionFactory;
    }
}

Notice the StandardServiceRegistryBuilder().applySettings call where configuration properties are passed. Hibernate is still evolving and every version comes with a lot of changes and I feel that documentation part is lagging. I hope this will help someone fixing the issue rather than wasting a lot of time debugging.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
March 23, 2019

Very useful article. Finally got my app working. Thank you.

- David Gesrib

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 5, 2017

    Hi Pankaj , I’m creating using below config but still getting same issue. Can you please help. import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.orm.hibernate4.HibernateTransactionManager; import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import java.util.Properties; @Configuration @EnableTransactionManagement @PropertySource({“classpath:pgsql.properties”}) @ComponentScan({“path”}) public class PersistenceConfig { @Autowired private Environment env; public PersistenceConfig() { super(); } @Bean public LocalSessionFactoryBean sessionFactory() { final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(getDataSource()); sessionFactory.setPackagesToScan(new String[]{“path”}); sessionFactory.setHibernateProperties(hibernateProperties()); return sessionFactory; } @Bean HikariDataSource getDataSource() { Properties props = new Properties(); props.setProperty(“dataSourceClassName”, “value”); props.setProperty(“dataSource.user”, “value”); props.setProperty(“dataSource.password”, “value”); props.setProperty(“dataSource.databaseName”, “value”); props.setProperty(“dataSource.serverName”, “value”); props.setProperty(“dataSource.portNumber”, “value”); //props.put(“dataSource.logWriter”, new PrintWriter(System.out)); HikariConfig config = new HikariConfig(props); return new HikariDataSource(config); } @Bean public PlatformTransactionManager hibernateTransactionManager() { final HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(sessionFactory().getObject()); return transactionManager; } private final Properties hibernateProperties() { final Properties hibernateProperties = new Properties(); hibernateProperties.setProperty(“hibernate.hbm2ddl.auto”, “create-drop”); hibernateProperties.setProperty(“hibernate.dialect”, “org.hibernate.dialect.PostgreSQLDialect”); hibernateProperties.setProperty(“hibernate.show_sql”, “true”); // hibernateProperties.setProperty(“hibernate.format_sql”, “true”); // hibernateProperties.setProperty(“hibernate.globally_quoted_identifiers”, “true”); // Envers properties // hibernateProperties.setProperty(“org.hibernate.envers.audit_table_suffix”, env.getProperty(“envers.audit_table_suffix”)); return hibernateProperties; } }

    - Mike

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      May 13, 2017

      thanks… it worked like a charm. U saved my night. :)

      - Shashank

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        December 23, 2015

        Thanks! It did help me, I was getting the same error. I didn’t understand the part that Hibernate is able to find configuration file, since it says “hibernate.dialect”, which implies opposite!

        - Ravi Arora

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          December 16, 2015

          Thank you all, You really saved my night!!! It works

          - mha

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            December 8, 2015

            I also wanted to express my thanks. I also found the hibernate documentation kind of weird… Amazingly one runs into this very problem, when you try to follow the “first app” section at the start of the documentation of version 4.3.11.FINAL. It surprises me, that this doesn’t get more attention. Also the documentation to version 5 is very incomplete… without any clear notice of that, or at least not clear enough for me to notice, until I compared the documentation with previous ones.

            - Marc

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              August 18, 2015

              Thank you.

              - raiym

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                August 9, 2015

                Thank you very much sir. You made my day

                - Soujanya

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  July 21, 2015

                  many thx mate your post just made my day! it’s weird that their developer didnt update their getting start example related to the evolving…

                  - djpath

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    June 15, 2015

                    Thanks for your post! It’s helped me a lot.

                    - Jonathan Campos

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Featured on Community

                      Get our biweekly newsletter

                      Sign up for Infrastructure as a Newsletter.

                      Hollie's Hub for Good

                      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                      Become a contributor

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      Welcome to the developer cloud

                      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Learn more