hibernateコアクラスとインタフェース--openSessionとgetCurrentSessionの思想
958 ワード
[size=medium]
[/size]
final public class HibernateUtil { //SqlHelper
[size=large]private static SessionFactory sessionFactory=null;
//
private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
private HibernateUtil(){};
static {
sessionFactory=new Configuration().configure("com/hsp/config/hsp.cfg.xml").buildSessionFactory();
}
// sesession
public static Session openSession(){
return sessionFactory.openSession();
}
// session
public static Session getCurrentSession(){
Session session=threadLocal.get();
//
if(session==null){
session=sessionFactory.openSession();
// session threadLocal, session
threadLocal.set(session);
}
return session;
}[/size]
[/size]