[已解决] HibernateException:当未设置“hibernate.dialect”时,对 DialectResolutionInfo 的访问不能为空
我按照Hibernate 官方文档创建了一个版本为4.3.5.Final的基本 Hibernate 应用程序。当我执行该应用程序时,hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
尽管所有配置对我来说似乎都很好,但我还是得到了。
HibernateException:当未设置“hibernate.dialect”时,对 DialectResolutionInfo 的访问不能为空
我花了很长时间才解决这个问题,结果发现问题出在SessionFactory
实例的创建方式上。我创建 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;
}
}
我得到了以下堆栈跟踪:
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)
从日志中可以清楚地看出,Configuration 类能够找到Hibernate配置文件并且已经hibernate.dialect
定义,因此异常没有任何意义。
修复 HibernateException:当未设置“hibernate.dialect”时,对 DialectResolutionInfo 的访问不能为空
经过一番研究和调试,我终于解决了这个问题。官方文档中缺少的部分是我们需要将配置属性应用于StandardServiceRegistryBuilder
。当我将实用程序类更改为以下内容时,一切都运行良好。
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;
}
}
注意StandardServiceRegistryBuilder().applySettings
传递配置属性的调用。Hibernate 仍在不断发展,每个版本都有很多变化,我觉得文档部分已经落后了。我希望这能帮助人们解决问题,而不是浪费大量时间进行调试。