`
jinxhj2003
  • 浏览: 145258 次
  • 性别: Icon_minigender_1
  • 来自: 南昌
社区版块
存档分类
最新评论

hibernateutil 得到hibernate

阅读更多
package com.jxsme.hibernate;
/*
* autho huangjin green eat 
*Oct 9, 2008
*/

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

public static final SessionFactory sessionFactory;

static {
try {

sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block

e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}

public static final ThreadLocal<Session> session = new ThreadLocal<Session>();

public static Session currentSession() throws HibernateException {
Session s = session.get();

if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}

return s;
}

public static void closeSession() throws HibernateException {
Session s = session.get();
if (s != null) {
s.close();
}
session.set(null);
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics