[已解决] HibernateException:当未设置'hibernate.dialect'时,访问DialectResolutionInfo不能为空

我按照Hibernate官方Documentation]创建了一个基本的Hibernate应用程序4.3.5最终版本 。当我执行应用程序时,我得到了hibernate.HibernateException:当‘hibernate.Dialect’没有设置时,对DialectResolutionInfo的访问不能为空,即使所有的配置在我看来都是正常的。

HibernateException:未设置'hibernate.dialect'时访问DialectResolutionInfo不能为null

我花了很长时间才解决了这个问题,结果是SessionFactory实例的创建方式有问题。我创建SessionFactory实例的实用程序类如下所示。

 1package com.journaldev.hibernate.util;
 2
 3import org.hibernate.SessionFactory;
 4import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 5import org.hibernate.cfg.Configuration;
 6import org.hibernate.service.ServiceRegistry;
 7
 8public class SessionFactoryUtil {
 9
10    private static SessionFactory sessionFactory;
11    
12    private static SessionFactory buildSessionFactory() {
13        try {
14            // Create the SessionFactory from hibernate.cfg.xml
15       	SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml")
16        				.buildSessionFactory(new StandardServiceRegistryBuilder().build());
17        	
18            return sessionFactory;
19        }
20        catch (Throwable ex) {
21            // Make sure you log the exception, as it might be swallowed
22            System.err.println("Initial SessionFactory creation failed." + ex);
23            throw new ExceptionInInitializerError(ex);
24        }
25    }
26    
27    public static SessionFactory getSessionFactory() {
28    	if(sessionFactory == null) sessionFactory = buildSessionFactory();
29        return sessionFactory;
30    }
31}

我得到了以下堆栈跟踪:

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

从日志中可以清楚地看到,Configuration类能够找到hibernate配置文件,并且它定义了hibernate.Dialt,因此exception没有任何意义。

修复HibernateException:未设置‘hibernate.Dialect’时,对DialectResolutionInfo的访问不能为空

经过一些研究和调试,我能够把它修好。官方文档中缺少的部分是,我们需要将配置属性应用到StandardServiceRegistryBuilder。当我将实用程序类更改为Below时,一切都非常顺利。

 1package com.journaldev.hibernate.util;
 2
 3import org.hibernate.SessionFactory;
 4import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 5import org.hibernate.cfg.Configuration;
 6import org.hibernate.service.ServiceRegistry;
 7
 8public class SessionFactoryUtil {
 9
10    private static SessionFactory sessionFactory;
11    
12    private static SessionFactory buildSessionFactory() {
13        try {
14            // Create the SessionFactory from hibernate.cfg.xml
15        	Configuration configuration = new Configuration();
16        	configuration.configure("hibernate.cfg.xml");
17        	System.out.println("Hibernate Configuration loaded");
18        	
19        	//apply configuration property settings to StandardServiceRegistryBuilder
20        	ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
21        	System.out.println("Hibernate serviceRegistry created");
22        	
23        	SessionFactory sessionFactory = configuration
24        						.buildSessionFactory(serviceRegistry);
25        	
26            return sessionFactory;
27        }
28        catch (Throwable ex) {
29            // Make sure you log the exception, as it might be swallowed
30            System.err.println("Initial SessionFactory creation failed." + ex);
31            throw new ExceptionInInitializerError(ex);
32        }
33    }
34    
35    public static SessionFactory getSessionFactory() {
36    	if(sessionFactory == null) sessionFactory = buildSessionFactory();
37        return sessionFactory;
38    }
39}

请注意StandardServiceRegistryBuilder().applySettings调用,其中传递了配置属性。Hibernate仍在发展,每个版本都有很多变化,我觉得文档部分很滞后。我希望这将有助于有人解决这个问题,而不是浪费大量的时间调试。

Published At
Categories with 技术
Tagged with
comments powered by Disqus