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]
}

org.hibernate.LazyInitializationException:could not initialize proxy - the owning Session was closed

03-04

hibernate出现异常,有知道的人,给个解决方案,谢谢rn积分奖励!rnrn2008-03-04 16:51:19 [日志信息] could not initialize proxy - the owning Session was closedrn[color=#3366FF]org.hibernate.LazyInitializationException[/color]: could not initialize proxy - the owning Session was closedrn at org.hibernate.proxy.AbstractLazyInitializer.initialize([color=#3366FF]AbstractLazyInitializer.java:53[/color])rn at org.hibernate.proxy.AbstractLazyInitializer.getImplementation([color=#3366FF]AbstractLazyInitializer.java:84[/color])rn at org.hibernate.proxy.CGLIBLazyInitializer.intercept([color=#3366FF]CGLIBLazyInitializer.java:134[/color])rn at com.orm.MemberLevel$$EnhancerByCGLIB$$54d60d0d.getLevelName()rn at sun.reflect.NativeMethodAccessorImpl.invoke0([color=#3366FF]Native Method[/color])rn at sun.reflect.NativeMethodAccessorImpl.invoke([color=#3366FF]NativeMethodAccessorImpl.java:39[/color])rn at sun.reflect.DelegatingMethodAccessorImpl.invoke([color=#3366FF]DelegatingMethodAccessorImpl.java:25[/color])rn at java.lang.reflect.Method.invoke([color=#3366FF]Method.java:585[/color])rn at org.apache.commons.el.ArraySuffix.evaluate([color=#3366FF]ArraySuffix.java:314[/color])rn at org.apache.commons.el.ComplexValue.evaluate([color=#3366FF]ComplexValue.java:145[/color])rn at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate([color=#3366FF]ExpressionEvaluatorImpl.java:263[/color])rn at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate([color=#3366FF]ExpressionEvaluatorImpl.java:190[/color])rn at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate([color=#3366FF]PageContextImpl.java:922[/color])rn at org.apache.jsp.default_jsp._jspx_meth_bean_message_7([color=#3366FF]org.apache.jsp.default_jsp:900[/color])rn at org.apache.jsp.default_jsp._jspx_meth_logic_present_1([color=#3366FF]org.apache.jsp.default_jsp:845[/color])rn at org.apache.jsp.default_jsp._jspService([color=#3366FF]org.apache.jsp.default_jsp:263[/color])rn at org.apache.jasper.runtime.HttpJspBase.service([color=#3366FF]HttpJspBase.java:97[/color])rn at javax.servlet.http.HttpServlet.service([color=#3366FF]HttpServlet.java:802[/color])rn at org.apache.jasper.servlet.JspServletWrapper.service([color=#3366FF]JspServletWrapper.java:322[/color])rn at org.apache.jasper.servlet.JspServlet.serviceJspFile([color=#3366FF]JspServlet.java:291[/color])rn at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)rn at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)rn at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)rn at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)rn at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)rn at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1085)rn at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:398)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:241)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)rn at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)rn at com.ricky.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:33)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)rn at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)rn at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)rn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)rn at java.lang.Thread.run(Thread.java:595)rn2008-03-04 16:51:19 [日志信息] Servlet.service() for servlet jsp threw exceptionrnorg.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closedrn at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)rn at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)rn at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)rn at com.orm.MemberLevel$$EnhancerByCGLIB$$54d60d0d.getLevelName()rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:585)rn at org.apache.commons.el.ArraySuffix.evaluate(ArraySuffix.java:314)rn at org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)rn at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:263)rn at org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:190)rn at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:922)rn at org.apache.jsp.default_jsp._jspx_meth_bean_message_7(org.apache.jsp.default_jsp:900)rn at org.apache.jsp.default_jsp._jspx_meth_logic_present_1(org.apache.jsp.default_jsp:845)rn at org.apache.jsp.default_jsp._jspService(org.apache.jsp.default_jsp:263)rn at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rn at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)rn at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)rn at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭