1. 程式人生 > >[Hibernate]Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

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

util create size dialect eat private 解決 代碼 ror

使用Hibernate官方文檔上的下面代碼進行測試時報出這個異常。

org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect‘ not set

package org.hibernate.tutorial.util;

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
            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;
    }

}


後來改動成舊版本號的例如以下代碼異常消失:

Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();

可是無參的buildSessionFactory方法在新版本號中已經不推薦使用了,最後找到了解決的方法:

Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build());


親測不會再出現這個異常了。



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