【发布时间】:2014-01-19 03:48:10
【问题描述】:
在hibernate documentation for version 4.3.0.Final中给出以下代码sn-p来创建SessionFactory:
package org.hibernate.tutorial.util;
import org.hibernate.SessionFactory;
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
return new Configuration().configure().buildSessionFactory();
}
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;
}
}
这似乎已经过时,因为 buildSessionFactory() 方法已被弃用。创建SessionFactory 的正确方法是什么?
【问题讨论】:
标签: java hibernate configuration sessionfactory