Hebernate 的SessionFactory怎么写,我的session.isOpen()报错,让buildPath
我的SessionFactory这样写的,写了两天了,也没写好,也不能完成基本的增删改查
package com.aisino.hibernate.source;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HebernateUtil {
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
//SessionFactory的对象
private static SessionFactory sessionFactory=null;
//静态块
static{
//加载Hibernate配置文件
Configuration cfg=new Configuration().configure();
//sessionFactory=cfg.buildSessionFactory(new ServiceRegistry());
}
//获取Session
public static Session getSession() throws HibernateException{
Session session=threadLocal.get();
if(session==null || !session.isOpen()){
rebuildSessionFactory();
}
session=(sessionFactory !=null)?sessionFactory.openSession():null;
threadLocal.set(session);
}
//重建会话工厂
private static void rebuildSessionFactory() {
// TODO Auto-generated method stub
}
//关闭Session
public static void closeSession() throws HibernateException {
Session session = threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
}