一尘不染

未设置“ hibernate.dialect”时,Hibernate 4对DialectResolutionInfo的访问不能为空

hibernate

我正在使用Hibernate最新版本4.3.5.Final

我的hibernate.cfg.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/Test</property>
    <property name="hibernate.connection.username">pankaj</property>
    <property name="hibernate.connection.password">xxxx</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <mapping resource="employee.hbm.xml"/>
</session-factory>

</hibernate-configuration>

创建SessionFactory的实用程序类:

package com.example;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

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");

        SessionFactory sessionFactory = configuration.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() {
    return sessionFactory;
}
}

在我的main()方法中使用它时,出现以下异常:

May 04, 2014 11:55:56 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Hibernate Configuration loaded
May 04, 2014 11:55:57 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.ExceptionInInitializerError
    at com.example.HibernateUtil.buildSessionFactory(HibernateUtil.java:25)
    at com.example.HibernateUtil.<clinit>(HibernateUtil.java:9)
    at com.example.HibernateMain.main(HibernateMain.java:26)
Caused by: 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:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at com.example.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
    ... 2 more

我尝试了很多选择,并寻找了在线资源,hibernate文档,但找不到此处缺少的内容。任何帮助表示赞赏。


阅读 264

收藏
2020-06-20

共1个答案

一尘不染

最终能够解决问题,问题在于SessionFactory创建方法。

首先,正式文件是第一个要去的地方,我必须说,Hibernate正式文件似乎没有更新为最新版本。

解决方法是将配置设置应用于StandardServiceRegistryBuilder实例。

// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
        .applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration
        .buildSessionFactory(serviceRegistry);

在此处参考我的博客文章以获取更多详细信息。

2020-06-20