在Spring+Hibernate中,用jUnit测试时候,提示the owning Session was closed
错误,
测试代码片段如下:
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(),"其他");
assertEquals(updateOperator.getDept().getName(),addDept.getName());
红色字体一行测试不通过,Session的生命周期结束,调用updateOperator.getDept().getName()失败。
环境:Spring2,Hibernate3,jdk1.5
========================================
修改如下,在测试类的setUp和tearDown方法加入下代码
protected void setUp() throws Exception {
super.setUp();
SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
protected void tearDown() throws Exception {
super.tearDown();
session.flush();
SessionFactoryUtils.releaseSession(session,sessionFactory);
}
分享到:
相关推荐
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed ``` - **日志记录**:为了更好地诊断问题,可以使用log4j等工具进行日志...
Could not initialize proxy - the owning Session was closed 这是Hibernate懒加载机制中的常见异常,当尝试访问一个已被关闭的Session中的懒加载属性时触发。例如,如果Session在使用`setFetchMode(FetchMode....
The availability of drivers that support the features of the specification will simplify the process of developing Windows appli¬cation programs that in-corporate absolute coordinate input, and ...
在IT行业中,Hibernate是一个非常重要的Java持久化框架,它简化了数据库操作,使得开发者可以更加专注于业务逻辑,而不是底层的数据访问细节。这篇博客"Hibernate关联关系疑问"可能探讨了在使用Hibernate处理对象...
标题和描述中提到的“Cohen-Game-Of-Chromes-Owning-The-Web-With-Zombie-Chrome-Extens”以及“Owning the Web with Zombie Chrome Extensions”涉及了一系列关于恶意Chrome浏览器扩展(称为“僵尸扩展”)的研究,...
Cohen-Game-Of-Chromes-Owning-The-Web-With-Zombie-Chrome-Extensions-wp
「安全资讯」hta-t07_modern-exploitation-owning-all-of-the-things - 数据安全 漏洞分析 漏洞预警 WEB应用防火墙 工控安全 安全管理
- **Bin篡改**:通过对Bin的数据结构进行篡改,使得jemalloc错误地释放或分配内存,进而引发内存破坏。 - **Chunk重定位**:利用内存布局和运行时条件,使得jemalloc误以为某个Chunk已被释放,然后再次使用该Chunk,...
"I have dreamed of being a singer." 和 "When he was a young man, he dreamt of going to Beijing." 都是用这个短语来表达个人的愿望或想象。值得注意的是,dream 的过去式和过去分词有两种形式:dreamed 和 ...
The top window always shows a list of the currently active processes, including the names of their owning accounts, whereas the information displayed in the bottom window depends on the mode that ...
The top window always shows a list of the currently active processes, including the names of their owning accounts, whereas the information displayed in the bottom window depends on the mode that ...
在IT行业中,尤其是在Java开发或者使用ORM框架(如Hibernate)时,“inverse=true”是一个非常重要的概念,它涉及到对象关系映射中的数据管理策略。本文将深入解析“inverse=true”的含义,以及它在实际应用中的作用...
Owning to my hard work, I was rewarded the Excellent Prize in the successive three years. However, with time going on, the more I experienced, the clearer I realized that I am really interested in ...
Camille Stewart在其演讲"Owning Your Professional Narrative"中强调了如何有效地管理和表达自己的专业故事,这对于网络安全专家尤其重要,因为这不仅关乎个人成长,也影响着安全工作的执行和效果。 首先,确定3-5...
Active Ports maps ports to the owning application so you can watch which process has opened which port. It also displays a local and remote IP address for each connection and allows you to terminate ...
The top window always shows a list of the currently active processes, including the names of their owning accounts, whereas the information displayed in the bottom window depends on the mode that ...
在Java的持久化框架Hibernate中,一对一(OneToOne)关系是一种常见的关联映射方式,尤其在处理数据库表之间一对一关联的场景下。本教程将详细探讨Hibernate中的双向外键(Bidirectional Foreign Key)配置,以及...
Active Ports maps ports to the owning application so you can watch which process has opened which port. It also displays a local and remote IP address for each connection and allows you to ...