Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

最近突然想回顾下Hibernate的一些常用操作,于是去网上下载了最新的hibernate版本包,于是把原来练习的代码拿出来运行,结果就发现buildSessionFactory这个方法hibernate的最新版本已经不推荐使用了,于是利用开发工具的提示功能,发现有一个对应的重载方法,于是拿出来使用。

代码如下(new StandardServiceRegistryBuilder().build()是在官方文档中找到的):

public static void main(String[] args) {
		Configuration cfg = new Configuration();
		cfg.configure("/hibernate.cfg.xml");// 加载配置信息

		SessionFactory factory = cfg
				.buildSessionFactory(new StandardServiceRegistryBuilder()
						.build());

		Session session = factory.getCurrentSession();

		Student stu = new Student(1, "student01", "male");

		Transaction tr = session.getTransaction();
		
		try {
			tr.begin();
			session.save(stu);
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();//此处如果外面获取的session用getCurrentSession()获取时则需要进行判断session是否已经关闭了,但是如果是openSession()获取的session的话,就不用判空了,因为他是没有关闭的。需要手动关闭。
		}
	}

发现废弃的告警已经消除了,好了,直接运行之后就发现出问题了。报了如下异常信息:

Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
	at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
	at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)
	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
	at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
	at com.fit.method.TestSaveOrUpdateMethod.main(TestSaveOrUpdateMethod.java:18)

但是在方言在hibernate.cfg.xml文件中已经配置了,如下配置,但是为什么还是报呢。

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
	<!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost/test</property>
    <property name="connection.username">root</property>
    <property name="connection.password">mb</property>
    <!-- SQL dialect(方言) -->
    <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <property name="current_session_context_class">thread</property>
    <mapping resource="com/fit/entity/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration
>

官方文档上也没搜索到,于是百度搜索了下,才发现少调用了一句话。

		SessionFactory factory = cfg
				.buildSessionFactory(new StandardServiceRegistryBuilder()
						.applySettings(cfg.getProperties()).build());

必须要把cfg中配置再向里面传递一次才可以。再次运行代码OK了。

PS:只能说一句,官方的文档也是过时了呀(网上很多人也在抱怨这个问题),用发布的最新的包中的文档里面的示例代码都不能运行成功,大公司也不一定那么权威么。。。

猜你喜欢

转载自beijishiqidu.iteye.com/blog/2199374
今日推荐