HibernateSessionFactory演示样例
Posted lxjshuju
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HibernateSessionFactory演示样例相关的知识,希望对你有一定的参考价值。
package common;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static Configuration cfg;
private static SessionFactory sessionFactory;
private HibernateSessionFactory() {}
static {
try {
cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch (HibernateException e) {
// 做日志
throw new RuntimeException("hibernate初始化错误", e);
}
}
public static Session getSession() {
return sessionFactory.getCurrentSession();
// return sessionFactory.openSession();
}
}
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static Configuration cfg;
private static SessionFactory sessionFactory;
private HibernateSessionFactory() {}
static {
try {
cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch (HibernateException e) {
// 做日志
throw new RuntimeException("hibernate初始化错误", e);
}
}
public static Session getSession() {
return sessionFactory.getCurrentSession();
// return sessionFactory.openSession();
}
}
以上是关于HibernateSessionFactory演示样例的主要内容,如果未能解决你的问题,请参考以下文章
如何理解Hibernate中的HibernateSessionFactory类
HibernateSessionFactory建立-使用ThreadLocal
如何理解Hibernate中的HibernateSessionFactory类
代码中的ThreadLocal在这个HibernateSessionFactory中啥作用?为啥还要重建sessionFactory?求助谢谢!
web项目运行到session = hibernateutil.HibernateSessionFactory.getSession()就说找不到error.jsp