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 Unsupported encoding: gb2312 錯誤原因
今天做了一個JSP頁面,運行時tomcat提示:org.apache.jasper.JasperException: Unsupported encoding: gb2312 錯誤,找了很久才找到出錯的地方,原來是一個空格惹的禍。2009-06-06
JSP實用教程之簡易頁面編輯器的實現(xiàn)方法(附源碼)
對于一些剛入門的程序員來說,在一些實際的開發(fā)中可能會用到編輯器,那么如何在JSP頁面中嵌入并使用編輯器,下面這篇文章就給大家介紹了JSP簡易頁面編輯器的實現(xiàn)方法,文中介紹的非常詳細,需要的朋友可以參考下。2017-07-07
Struts1之url截取_動力節(jié)點Java學院整理
這篇文章主要介紹了Struts1之url截取_動力節(jié)點Java學院整理的相關資料,需要的朋友可以參考下2017-09-09

