import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
public class MySessionFactory
{
private static Configuration cfg = new Configuration();
private static SessionFactory sf = null;
private static ThreadLocal<Session> threadlocal = new ThreadLocal<Session>();
static{
cfg.configure("hibernate.cfg.xml");
ServiceRegistryBuilder srb = new ServiceRegistryBuilder().applySettings(cfg.getProperties());
sf = cfg.buildSessionFactory(srb.buildServiceRegistry());
}
public static Session get_current_session()
{
Session session = (Session) threadlocal.get();
if(session==null || !session.isOpen())
{
session = sf.getCurrentSession();
threadlocal.set(session);
}
return session;
}
public static void close_session()
{
Session session = (Session) threadlocal.get();
if(session != null)
threadlocal.set(null);
}
}
Hibernate 4中buildSessionFactory()过时
最新推荐文章于 2017-11-14 14:31:00 发布