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.
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.
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.
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.
Very useful article. Finally got my app working. Thank you.
- David Gesrib
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
thanks… it worked like a charm. U saved my night. :)
- Shashank
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
Thank you all, You really saved my night!!! It works
- mha
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
Thank you.
- raiym
Thank you very much sir. You made my day
- Soujanya
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
Thanks for your post! It’s helped me a lot.
- Jonathan Campos