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.
我正在遵循Hibernate官方文档来创建版本为4.3.5.Final的基本hibernate应用程序。 当我执行该应用程序时,我得到了hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
即使对我来说所有配置都很好,但hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
。
HibernateException:如果未设置“ hibernate.dialect”,则对DialectResolutionInfo的访问不能为null (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.
我花了很长时间解决问题,事实证明这与创建SessionFactory
实例的方式有关。
My utility class to create SessionFactory instance was like below.
我的创建SessionFactory实例的实用程序类如下。
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.
从日志中可以明显看出,Configuration类能够找到Hibernate配置文件,并且已定义hibernate.dialect
,因此该异常没有任何意义。
修复HibernateException:未设置'hibernate.dialect'时对DialectResolutionInfo的访问不能为null (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
.
经过一些研究和调试,我能够修复它。 官方文档中缺少的部分是,我们需要将配置属性应用于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.
注意,在其中传递配置属性的StandardServiceRegistryBuilder().applySettings
调用。
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.
Hibernate仍在不断发展,每个版本都有很多更改,我觉得文档部分很滞后。 我希望这将有助于某人解决此问题,而不是浪费大量的调试时间。