HibernateUtil + MySessionFactory
2013-04-09 16:48
295 查看
package com.yyy.util;
//import org.hibernate.Session;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
final public class HibernateUtil {
private static SessionFactory sessionFactory = null;
private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
public HibernateUtil(){
}
static{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
//获取全新的session
public static Session openSession(){
return sessionFactory.openSession();
}
//获取和线程关联的session
public static Session getCurrentSession(){
Session session = threadLocal.get();
//判断是否得到
if(session == null){
session = sessionFactory.openSession();
//session对象设置到threadLocal -- 相当于session和线程绑定
threadLocal.set(session);
}
return session;
}
}
MySessionFactory
package com.yyy.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
//使用hibernate请保证只有一个SessionFactory(一个数据库)
final public class MySessionFactory {
private static SessionFactory sessionFactory = null;
//1创建configuration对象
// Configuration configuration = new Configuration().configure();
//2 //2.创建会话工厂、SessionFactory》》重量级对象。
// SessionFactory sessionFactory = configuration.buildSessionFactory();
public MySessionFactory(){
}
static {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getsessionFactory(){
return sessionFactory;
}
}
//import org.hibernate.Session;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
final public class HibernateUtil {
private static SessionFactory sessionFactory = null;
private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
public HibernateUtil(){
}
static{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
//获取全新的session
public static Session openSession(){
return sessionFactory.openSession();
}
//获取和线程关联的session
public static Session getCurrentSession(){
Session session = threadLocal.get();
//判断是否得到
if(session == null){
session = sessionFactory.openSession();
//session对象设置到threadLocal -- 相当于session和线程绑定
threadLocal.set(session);
}
return session;
}
}
MySessionFactory
package com.yyy.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
//使用hibernate请保证只有一个SessionFactory(一个数据库)
final public class MySessionFactory {
private static SessionFactory sessionFactory = null;
//1创建configuration对象
// Configuration configuration = new Configuration().configure();
//2 //2.创建会话工厂、SessionFactory》》重量级对象。
// SessionFactory sessionFactory = configuration.buildSessionFactory();
public MySessionFactory(){
}
static {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getsessionFactory(){
return sessionFactory;
}
}
相关文章推荐
- HibernateSessionFactoryUtil.java(Hibernate工具类)
- HibernateSessionFactoryUtil
- Hibernate4 buildSessionFactory过时解决方案HibernateUtil
- Hibernate4.3.8 buildSessionFactory过时解决方案HibernateUtil
- required a bean of type 'org.hibernate.SessionFactory' that could not be found.
- SSH整合,"sessionFactory " or "hibernateTemplate " is required异常
- Hibernate SessionFactory配置实例
- java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.springfram
- getHibernateTemplate和getSession 区别, this.getHibernateTemplate().getSessionFactory().getCurrentSession()和OpenSession区别
- hibernate sessionFactory(转)
- No bean named 'MysessionFactory' is defined问题的解决
- java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplemen
- spring事务配置Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplemento
- 最近用STS搭建hibernate&spring,遇到Could not locate SessionFactory in JNDI.找到这个文章很快解决了
- hibernatesessionfactory
- Hibernate创建buildSessionFactory()之后的代码都不执行
- SSH整合,"sessionFactory " or "hibernateTemplate " is required異常 继承HibernateDaoSupport
- 【已解决】NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()...
- Hibernate运行错误之java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getPro
- buildSessionFactory 和 ServiceRegistryBuilder in Hibernate 4.3.4 被弃用.