国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

SSH整合中 hibernate托管給Spring得到SessionFactory

2024-09-05 00:21:13
字體:
來源:轉載
供稿:網友
Spring文件中的 SessionFactory中 加入為了能得到同一個Session

<prop key="hibernate.current_session_context_class">thread</prop>
然后
Resource resource=new ClassPathResource("/WEB-INF/applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
SessionFactory sessionFactory = (SessionFactory)factory.getBean("sessionFactory");
就可以得到了
剩下的 不會就回爐吧,我 的 做法是 修改HibernateUtil文件的得到SessionFactory 的方法就 什么都解決了
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
//在hibernate托管給Spring時得到sessionFactory
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Resource resource=new ClassPathResource("/WEB-INF/applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
sessionFactory = (SessionFactory)factory.getBean("sessionFactory");
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: "
+ ex.getMessage(), ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
//
當hibernate沒有托管給Spring使用這種和傳統方式都可以得到啊
sessionFactory = new Configuration().configure("/WEB-INF/hibernate.cfg.xml")
.buildSessionFactory();

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广西| 忻州市| 沾益县| 克什克腾旗| 灌云县| 永善县| 合山市| 和政县| 山阴县| 萝北县| 嘉兴市| 磐安县| 西宁市| 泸州市| 呼和浩特市| 安平县| 博乐市| 项城市| 方山县| 建阳市| 甘德县| 商都县| 即墨市| 分宜县| 房产| 衡东县| 绿春县| 吉木乃县| 禹城市| 汝城县| 永安市| 云和县| 台南县| 金堂县| 济源市| 靖宇县| 赤峰市| 台东县| 措美县| 巩义市| 栾城县|