SSH整合中 hibernate托管給Spring得到SessionFactory
更新時間:2009年06月18日 21:43:52 作者:
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使用這種和傳統(tǒng)方式都可以得到啊
sessionFactory = new Configuration().configure("/WEB-INF/hibernate.cfg.xml")
.buildSessionFactory();
然后
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使用這種和傳統(tǒng)方式都可以得到啊
sessionFactory = new Configuration().configure("/WEB-INF/hibernate.cfg.xml")
.buildSessionFactory();
您可能感興趣的文章:
- 詳解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
- SSH框架網(wǎng)上商城項目第1戰(zhàn)之整合Struts2、Hibernate4.3和Spring4.2
- Spring4整合Hibernate5詳細步驟
- spring+hibernate 兩種整合方式配置文件的方法
- Java框架篇:Spring+SpringMVC+hibernate整合開發(fā)
- Spring 整合 Hibernate 時啟用二級緩存實例詳解
- Spring+Hibernate+Struts(SSH)框架整合實戰(zhàn)
- Spring與Hibernate整合事務管理的理解
- Spring MVC+FastJson+hibernate-validator整合的完整實例教程
- struts+spring+hibernate三個框架的整合
- Spring和Hibernate的整合操作示例
相關文章
JSP使用Servlet作為控制器實現(xiàn)MVC模式實例詳解
這篇文章主要介紹了JSP使用Servlet作為控制器實現(xiàn)MVC模式的方法,以完整實例形式較為詳細的分析了MVC模式的原理及Servlet實現(xiàn)MVC模式的原理與相關注意事項,需要的朋友可以參考下2015-09-09JSP開發(fā)中Apache-HTTPClient 用戶驗證的實例詳解
這篇文章主要介紹了JSP開發(fā)中Apache-HTTPClient 用戶驗證的實例詳解的相關資料,這里提供實例幫助大家學習理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08springMVC使用jsp:include嵌入頁面的兩種方法(推薦)
下面小編就為大家?guī)硪黄猻pringMVC使用jsp:include嵌入頁面的兩種方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06用fileupload組件實現(xiàn)的大文件上傳簡單實例
下面小編就為大家?guī)硪黄胒ileupload組件實現(xiàn)的大文件上傳簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10JSP由淺入深(10)—— Beans and Forms處理
JSP由淺入深(10)—— Beans and Forms處理...2006-10-10