1. 程式人生 > >Hibernate4.3.x Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

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

          如下錯誤

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

                        今天沒事看看新的hibernate-core-4.3.10.Final   看來官方文件。

public class Person {

   public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPassword() { return password; } public void setPassword(int password) { this.password = password; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; }    private Integer id;    private String name;    private int password;    private Date birthday; }



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 持久化對映檔案(將java物件對映到資料庫表) -->
<hibernate-mapping>
   <class name="com.it.hibernate.pojo.Person" table="t_person">
     <id name="id">
        <generator class="native"></generator>
     </id>
     <property name="name" column="t_name" length="12"/>
     <property name="password" length="6"/>
     <property name="birthday" />
   </class> 
</hibernate-mapping>
官方獲取sessionfactory
public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory(
			    new StandardServiceRegistryBuilder().build() );
        }
        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;
    }

}
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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 資料庫方言 加五提高速度 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 展現sql -->
<property name="show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 自動建立sql create 每次執行都都要先刪除表再建立表 update 沒有就建立表 有就直接插入 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/it/hibernate/pojo/Person.hbm.xml" />
</session-factory>

</hibernate-configuration>


測試類

public class PersonTest {
public static void main(String[] args) {
  SessionFactory factory =HibernateUtil.getSessionFactory();
  Session session = factory.openSession();
  Transaction tx =   session.beginTransaction();
  Person p = new Person();
  p.setName("MOMO");
  p.setPassword(123456);
  p.setBirthday(new Date());
  session.save(p);
  tx.commit();
  session.close();
}
 
}
如下錯誤
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.it.hibernate.utils.HibernateUtil2.buildSessionFactory(HibernateUtil2.java:21)
at com.it.hibernate.utils.HibernateUtil2.<clinit>(HibernateUtil2.java:9)
at com.it.hibernate.pojo.test.PersonTest.main(PersonTest.java:14)
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:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at com.it.hibernate.utils.HibernateUtil2.buildSessionFactory(HibernateUtil2.java:16)
... 2 more
修改獲取sessionFactory的公用方法
public class HibernateUtil {


    private static final SessionFactory sessionFactory = buildSessionFactory();


    private static SessionFactory buildSessionFactory() {
        try {
            // 從 hibernate.cfg.xml 建立SessionFactory
        
        Configuration cfg = new Configuration().configure();
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(cfg.getProperties()).build();
            return cfg.buildSessionFactory(
            serviceRegistry);
        }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }


    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }


}
修改完就可以順利插入資料。 有個疑問是官方給錯了?還是我其他地方有問題有問題呢! 但是我改了sessionfactory獲取方式就好了呀!求大神指點。
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory(
			    new StandardServiceRegistryBuilder().build() );
        }
        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;
    }

}