[已解决] HibernateException:当未设置“ hibernate.dialect”时,对DialectResolutionInfo的访问不能为null

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仍在不断发展,每个版本都有很多更改,我觉得文档部分很滞后。 我希望这将有助于某人解决此问题,而不是浪费大量的调试时间。

翻译自: https://www.journaldev.com/2897/hibernateexception-access-dialectresolutioninfo-cannot-null-hibernate-dialect-not-set

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值