Hibernate openSession() 和 getCurrentSession的区别 .
来源:互联网 发布:mac向u盘复制文件 编辑:程序博客网 时间:2024/04/30 05:12
getHiberanteTemplate 、getCurrentSession和OpenSession
采用getCurrentSession()创建的Session会绑定到当前的线程中去、而采用OpenSession()则不会。
采用getCurrentSession()创建的Session在commit或rollback后会自动关闭,采用OpenSession()必须手动关闭。
采用getCurrentSession()需要在Hibernate.cfg.xml配置文件中加入如下配置:
如果是本地事物,及JDBC一个数据库:
<propety name=”Hibernate.current_session_context_class”>thread</propety>
如果是全局事物,及jta事物、多个数据库资源或事物资源:
<propety name=”Hibernate.current_session_context_class”>jta</propety>
使用spring的getHiberanteTemplate 就不需要考虑事务管理和session关闭的问题:
public List getEntityCriteria(final DetachedCriteria detachedCriteria) {
return (List) getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
Criteria criteria = detachedCriteria
.getExecutableCriteria(session);
return criteria.list();
}
});
}
public List getEntityCriteriaByPage(final DetachedCriteria detachedCriteria,final Page page) {
return (List) getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
Criteria criteria = detachedCriteria
.getExecutableCriteria(session);
criteria.setFirstResult(page.getFirstItemPos());
criteria.setMaxResults(page.getPageSize());
return criteria.list();
}
});
}
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Department.class);
detachedCriteria.add(Restrictions.eq("eid", seacher.getEid()));//企业id为条件查询
detachedCriteria.add(Restrictions.in("dprtid", ids));//根据id数组查询部门
detachedCriteria.add(Restrictions.like("name", "%" + seacher.getName()+ "%"));//名称模糊查询
getEntityCriteriaByPage(detachedCriteria,pageinfo);
经过检查激活连接为0,被使用的连接总是为1(应为自己测试)
package
com.myj.hibernate.model;
import
org.hibernate.HibernateException;
import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.AnnotationConfiguration;
import
org.hibernate.classic.Session;
import
org.junit.AfterClass;
import
org.junit.BeforeClass;
import
org.junit.Test;
public
class
TeacherTest {
public
static
SessionFactory sessionFactory =
null
;
@BeforeClass
public
static
void
beforeClass() {
try
{
sessionFactory =
new
AnnotationConfiguration().configure().buildSession Factory();
}
catch
(HibernateException e) {
e.printStackTrace();
}
}
@Test
public
void
testTeacherSave() {
Teacher teacher =
new
Teacher();
teacher.setName(
"martian"
);
teacher.setAge(
23
);
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(teacher);
//测试getCurrentSession()方法,这里输出true,因为在一个事务内,所以取得线程中的session
Session session1 = sessionFactory.getCurrentSession();
System.out.println(session == session1);
//commit()之后不用close(),假如使用sessionFactory.openSession();就需要close();
session.getTransaction().commit();
//测试getCurrentSession()方法,这输出false,上一个事务已经提交,这里将重新生成一个session
Session session2 = sessionFactory.getCurrentSession();
System.out.println(session == session2);
}
@AfterClass
public
static
void
afterClass() {
sessionFactory.close();
}
}
- hibernate 的openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- Hibernate 中 openSession()和getCurrentSession() 的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate之openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate: openSession() 和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别 .
- Hibernate的openSession和getCurrentSession区别
- hibernate中 openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- 【Hibernate】OpenSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate getCurrentSession()和openSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- 并查集详解
- linux下/dev/dsp:No such file or directory解决方法
- 编译第一步-lunch
- swift获取手机通讯录列表
- Android多线程--Handler
- Hibernate openSession() 和 getCurrentSession的区别 .
- java 高级特性——注解
- 两台linux服务器相互拷贝文件的两个方法
- double与flaot在杭电时的区别
- java socket 编程
- vs2010下配置pthread环境详解
- mysql 连接字符串与SQL不同
- Objective - C类的扩展
- 【笔记】mac配置文件