org.hibernate.LazyInitializationException: failed to lazily initialize a collection..的解决方案

原文地址:https://blog.csdn.net/u010504064/article/details/47832721

使用hibernate从一方获取多方信息的时候发生:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.haowei.carmanager.model。。。。
  • 1

从网上搜一下大致跟hibernate加载关联对象的方式有关,一个是懒加载方式,一个是立即加载方式。 
原先配置是:

一方中的多方:

@OneToMany(mappedBy = "carFirm",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private Set<CarBrandType> brandTypeSet;    //汽车厂商与汽车品牌为一对多的关系
  • 1
  • 2

多方中的一方:

@ManyToOne(cascade = CascadeType.ALL,optional = false)
    @JoinColumn(name="brandid",referencedColumnName = "carfirm_id")
    private CarFirm carFirm;             //汽车厂商与汽车品牌为一对多的关系
  • 1
  • 2
  • 3

注意在多方中的一方的配置没有指定加载方式,而且一方中的多方是懒加载,因此在获取一方的时候一方是获取到了,但是再获取多方的时候session已经关闭了,这时候会获取不到多方信息,因此报错。 
解决方式: 
一方中的多方:

@OneToMany(mappedBy = "carFirm",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    private Set<CarBrandType> brandTypeSet;    //汽车厂商与汽车品牌为一对多的关系
  • 1
  • 2

多方中的一方:

@ManyToOne(cascade = CascadeType.ALL,optional = false,fetch = FetchType.LAZY)
    @JoinColumn(name="brandid",referencedColumnName = "carfirm_id")
    private CarFirm carFirm;             //汽车厂商与汽车品牌为一对多的关系
  • 1
  • 2
  • 3

就是将一方中的多方的加载方式改为立刻加载,而多方中的一方改为懒加载即可。至于多方中的一方的加载方式是不是也跟该异常有关,这里就不实验了。


org.hibernate.LazyInitializationException: failed to lazily initialize a collect

07-19

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.jbpm.graph.def.Node.leavingTransitions, no session or session was closedrn at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)rn at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)rn at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)rn at org.hibernate.collection.PersistentList.size(PersistentList.java:91)rn at org.jbpm.graph.def.Node.getDefaultLeavingTransition(Node.java:235)rn at org.jbpm.graph.exe.Token.signal(Token.java:137)rn at org.jbpm.graph.exe.ProcessInstance.signal(ProcessInstance.java:270)rn at com.sino.bpm.pao.GeneralJbpmPAO.next(GeneralJbpmPAO.java:73)rn at com.sino.bpm.service.ProcessServiceSupport.agree(ProcessServiceSupport.java:60)rn at com.sino.bus.action.SecLevDeclareAction.audit(SecLevDeclareAction.java:115)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:597)rn at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:413)rn at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:134)rn at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:310)rn at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:297)rn at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)rn at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)rn at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)rn at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at com.sino.web.filters.GZipfilter.doFilter(GZipfilter.java:26)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)rn at org.apache.coyote.http11.Http11NioProcessor.process(Http11NioProcessor.java:879)rn at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:719)rn at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:2080)rn at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)rn at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)rn at java.lang.Thread.run(Thread.java:619)

failed to lazily initialize a collection of role

07-10

我在查询后给2个List集合转JSON字符串报如下异常。rnrn7 ERROR [2012-07-10 14:59:26] failed to lazily initialize a collection of role: com.art.model.ClassInfo.classLikeRecords, no session or session was closedrnorg.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.art.model.ClassInfo.classLikeRecords, no session or session was closedrn at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)rn at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)rn at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)rn at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)rn at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:434)rn at java.util.HashMap.put(HashMap.java:372)rn at java.util.HashSet.add(HashSet.java:200)rn at net.sf.json.AbstractJSON.addInstance(AbstractJSON.java:52)rn at net.sf.json.JSONArray._fromCollection(JSONArray.java:821)rn at net.sf.json.JSONArray.fromObject(JSONArray.java:137)rn at net.sf.json.JSONObject._processValue(JSONObject.java:2402)rn at net.sf.json.JSONObject._setInternal(JSONObject.java:2447)rn at net.sf.json.JSONObject.setValue(JSONObject.java:1189)rn at net.sf.json.JSONObject._fromBean(JSONObject.java:725)rn at net.sf.json.JSONObject.fromObject(JSONObject.java:182)rn at net.sf.json.JSONArray._processValue(JSONArray.java:2294)rn at net.sf.json.JSONArray.processValue(JSONArray.java:2325)rn at net.sf.json.JSONArray.addValue(JSONArray.java:2312)rn at net.sf.json.JSONArray._fromCollection(JSONArray.java:841)rn at net.sf.json.JSONArray.fromObject(JSONArray.java:137)rn at net.sf.json.JSONArray._processValue(JSONArray.java:2269)rn at net.sf.json.JSONArray.processValue(JSONArray.java:2325)rn at net.sf.json.JSONArray.addValue(JSONArray.java:2312)rn at net.sf.json.JSONArray.element(JSONArray.java:1488)rn at net.sf.json.JSONArray.add(JSONArray.java:1035)rn at net.sf.json.JSONArray.add(JSONArray.java:1031)rn at com.art.util.ARTUtils.toJSONString(ARTUtils.java:40)rn at com.art.action.ClassInfoAction.more(ClassInfoAction.java:234)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:597)rn at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)rn at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)rn at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)rn at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)rn at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)rn at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)rn at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)rn at java.lang.Thread.run(Thread.java:619)rnrn网上的解决方案是在一对多rn rn rn rn 接受者,针对哪门课程的人气rn rn rn rn rn配置lazy=false. 我测试过了,这能行得通,但是在我的classInfo里面有很多个这样的关系,而且其他表的配置也要加,如果都要这样配置,那么查询的效率就低得令人发指。那和出异常也没什么分别,求教还有什么解决方案.谢谢

failed to lazily initialize a collection of role: com.huawei.ciweb.entity.po.Rnd

08-13

2013-08-13 14:33:42 [ERROR] (LazyInitializationException.java:42) - failed to lazily initialize a collection of role: com.huawei.ciweb.entity.po.RndlteCiVersion.rndlteCiBrokens, no session or session was closedrnorg.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.huawei.ciweb.entity.po.RndlteCiVersion.rndlteCiBrokens, no session or session was closedrn at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)rn at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)rn at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)rn at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)rn at org.hibernate.collection.PersistentSet.equals(PersistentSet.java:429)rn at java.util.Vector.indexOf(Vector.java:361)rn at java.util.Vector.contains(Vector.java:320)rn at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:110)rn at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:363)rn at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:223)rn at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:171)rn at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:161)rn at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:127)rn at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:363)rn at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:223)rn at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:171)rn at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:161)rn at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:127)rn at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:363)rn at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:223)rn at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:171)rn at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:161)rn at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:127)rn at org.apache.struts2.json.JSONWriter.write(JSONWriter.java:95)rn at org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:116)rn at org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:196)rn at org.apache.struts2.json.JSONResult.execute(JSONResult.java:170)rn at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:374)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:278)rn at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)rn at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)rnrnrnrnrnrnrnrnrnrn rn rn rn rn rn rn rn 版本号rn rn rn rn rn 俗名rn rn rn rn rn 版本经理或软件经理rn rn rn rn rn 版本的所有接口人rn rn rn rn rn 域rn rn rn rn rn 软件包Summary.xml文件共享路径rn rn rn rn rn 冒烟Summary.xml文件共享路径rn rn rn rn rn 适配层Summary.xml文件共享路径rn rn rn rn rn 资料Summary.xml文件共享路径rn rn rn rn rn 配套工具Summary.xml文件共享路径rn rn rn rn rn 实时构建工程共享目录rn rn rn rn rn 无线度量链接rn rn rn rn rn CI度量的地址rn rn rn rn rn CI度量的地址rn rn rn rn rn 1:展示在主页;0:不展示在主页rn rn rn rn rn rn 版本IDrn rn rn rn rn rn rn rn rn 责任组rn rn rn rn rn rn rn rn 版本外键rn rn rn rn rn rnrnrnrnrnrnrnrnrnrnrn rn rn rn rn rn rn rn 责任组rn rn rn rn rn rn 版本rn rn rn rn rn 版本打断时间rn rn rn rn rn 版本修复时间rn rn rn rn rn 失败原因rn rn rn rn rn 反馈人rn rn rn rn rn 反馈时间rn rn rn rnrnrnrnrn帮忙看下什么原因,我在 many-to-one 加了 lazy="false"

hibernate多对多关联下的failed to lazily initialize a collection of role

12-27

RTrn我采用的是基于annotation的ssh2框架rn用MySQL创建好表之后逆向工程用hibernate生成了entity类和dao类rn其中有user表与follow表 follow表达了user与user之间的多对多关联rnrn在我的网页中 需要user A 关注(follow)user Brn而hibernate生成的类中没有follow类 而是在user类中有两个set 分别表示follow与被followrnrn每次在按下按钮 执行给user中的某个set增加一项 的操作时 都会被告知failed to lazily initialize a collection of rolernrn自己查了些情况rn有说强制把lazy属性修改的(已尝试 他的问题稍后描述)rn有说强行启动session 例如hibernate.initial();的语句rn但是由于我是用Spring管理hibernate 于是我只有applicationContext.xml这个配置文件 没有hibernate.cfg.xml这个配置文件 关于启动session 或者sessionfactory估计都不能用与hibernate.cfg.xml相关的方法来操作 尝试无果rnrn另 在退而求其次的使用了改lazy属性之后rn遇到的问题就是如何使用dao类来进行修改一个实例rnsave表示增加 delete表示修改 merge和attach等并不很清楚他们的作用rn我需要做的事findby得到这个实例 然后对他的某属性(类型是set)进行add操作 然后把该实例返回到数据库中 但是我直接save会被表示已存在相同pk的值 delete后save依然失败 修改save函数使save()的语句变为saveorupdate(0也没有用 尝试了一下merge等 也是报出奇怪的错误rnrn特此相求 希望有答

failed to lazily initialize a collection of role: no session or session was closed

06-07

我在启动TOMCAT 通过一个来初始化一些数据,碰到了这么一个问题,启动后访问都很正常,就是启动时出现这种问题。rnrn在init 方法中如下:rnServletContext scx = getServletContext(); rnWebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(scx);rnCustomListManager customListManager =(CustomListManager)wac.getBean("customListManager");rnList customLists = customListManager.getAll();rnrncustomLists 这个对象取到了customlist表中的所有记录,这里都很正常,但是接下出现了一奇怪的问题rnrn在getItems()时,出现了org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.aata.setup.CustomList.items, no session or session was closedrn不清楚,什么关闭session的,我的session是由OpenSessionInViewFilter控制的。rnfor(CustomList customList:customLists)rn System.out.print(customList.getItems().size());rnrnrnrnInitServlet.javarnrnpackage com.aata.web.common;rnrnimport java.util.List;rnrnimport javax.servlet.ServletContext;rnimport javax.servlet.ServletException;rnimport javax.servlet.http.HttpServlet;rnrnimport org.springframework.web.context.WebApplicationContext;rnimport org.springframework.web.context.support.WebApplicationContextUtils;rnrnimport com.aata.setup.CustomList;rnimport com.aata.setup.CustomListCache;rnimport com.aata.setup.CustomListManager;rnrnpublic class InitServlet extends HttpServlet rnrn private static final long serialVersionUID = 102294050344951035L;rn rn @Overridern public void init() throws ServletException rn ServletContext scx = getServletContext();rn rn WebApplicationContext wac = WebApplicationContextUtilsrn .getRequiredWebApplicationContext(scx);rn rn CustomListManager customListManager =(CustomListManager)wac.getBean("customListManager");rn List customLists = customListManager.getAll();rn for(CustomList customList:customLists)rn System.out.print(customList.getItems().size());rn rn rn rn CustomListCache.init(customListManager.getAll());rn rn rn CustomList country = CustomListCache.getCustomList(CustomListCache.COUNTRY);rn CustomList state = CustomListCache.getCustomList(CustomListCache.STATE);rn rn rn //set to servlet contextrn rn scx.setAttribute("countryList", country.getItems());rn scx.setAttribute("stateList", state.getItems()); rn rn

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

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