Spring+Hibernate中(the owning Session was closed错误)

在Spring+Hibernate中,用jUnit测试时候,提示the owning Session was closed
错误,
测试代码片段如下:
[color=indigo]Operator operator = createOperator();
Dept dept = createDept();
//保存部门
Integer deptCode = deptDao.saveDept(dept);
Dept addDept = deptDao.getDeptByCode(deptCode);
//保存新的用户
operator.setDept(addDept);
Integer userNo = operatorDao.saveOperator(operator);
//查询用户
Operator addOperator = operatorDao.getOperatorByCode(userNo);
assertEquals(addOperator.getUserName(),"test");
assertEquals(addOperator.getDept().getName(),"业务部门");
//更新
addOperator.setCname("其他");
operatorDao.saveOperator(addOperator);

Operator updateOperator = operatorDao.getOperatorByCode(userNo);
assertEquals(updateOperator.getCname(),"其他");
[/color]

[color=red]assertEquals(updateOperator.getDept().getName(),addDept.getName());[/color]
红色字体一行测试不通过,Session的生命周期结束,调用updateOperator.getDept().getName()失败。
环境:Spring2,Hibernate3,jdk1.5
========================================
修改如下,在测试类的setUp和tearDown方法加入下代码
protected void setUp() throws Exception {
super.setUp();
[color=red]SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));[/color]
}

protected void tearDown() throws Exception {
super.tearDown();
[color=red]session.flush();
SessionFactoryUtils.releaseSession(session,sessionFactory);[/color]
}
阅读更多
文章标签: Spring Hibernate junit
个人分类: Hibernate
下一篇整合spring2 + struts1.2 + hibernate3(转载)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭