[Solved] 当未设置 ‘hibernate.dialect’ 时,无法将 DialectResolutionInfo 访问为 null 的 HibernateException
我按照Hibernate官方文档创建了一个使用4.3.5.Final版本的基本Hibernate应用程序。当我执行该应用程序时,尽管所有配置看起来都很好,但我却收到了hibernate.HibernateException: 当’hibernate.dialect’未设置时,无法访问DialectResolutionInfo的错误信息。
当未设置“hibernate.dialect”时,无法将访问DialectResolutionInfo设置为null的Hibernate异常。
解决这个问题花了我很长时间,结果发现是SessionFactory实例创建方式的问题。我用下面的方法创建SessionFactory实例的辅助类。
package com.Olivia.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.Olivia.hibernate.main.HibernateMain.main(HibernateMain.java:38)
从日志中可以明确看出,Configuration类能够找到hibernate配置文件,并且已经定义了hibernate.dialect,所以异常没有任何意义。
当未设置‘hibernate.dialect’时,修复HibernateException:访问DialectResolutionInfo不能为空。
经过一些研究和调试,我成功修复了它。在官方文件中缺失的部分是我们需要将配置属性应用于StandardServiceRegistryBuilder。当我把实用类更改如下,一切都顺利运行。
package com.Olivia.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仍在不断发展,每个版本都带来了许多变化,我觉得文档部分滞后了。希望这能帮助某人解决问题,而不是浪费很多时间进行调试。