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

一个人会遇到许许多多的困难,可问题是坚持下去还是放弃。可能很多人都会选择放弃。可是放弃的人永远都会呆在别人的嘲笑当中,而坚持下去的人不管有没有成功,都会受到所有人的尊重。
<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();

以上就是SSH整合中 hibernate托管给Spring得到SessionFactory。曾经有人告诉过我,如果一个人真正的爱你,你是能够感觉得到的。在多次的感觉错误了之后,才发现原来,这句话意思是,真正爱你的人,不会要求你改变自己的模样去迎合他。更多关于SSH整合中 hibernate托管给Spring得到SessionFactory请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Hibernate 修改数据的实例详解

Hibernate 主清单文件配制的详细介绍

JSP开发之hibernate之单向多对一关联的实例

Hibernate识别数据库特有字段实例详解

JSP 开发之hibernate配置二级缓存的方法