本文介绍了org.apache.struts2.json.JSONException:org.hibernate.LazyInitializationException:未能延迟初始化集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我从jsp url:ProductByName调用autocomplete。 在控制台响应时显示 POST http:// localhost:8082 / Store / SingleProduct 500服务器错误) 在jsp中 source:function(request,response){ $ .ajax({ url:ProductByName , dataType:json, data:{ str:request.term, maxRows:5 },... 在 struts.xml中 < action name =ProductByNameclass =iland.productdetails.ProductDetailsActionmethod =fetchByProductName> < result type =json > < param name =includeProperties> productList \ [\ d + \] \.id, productList \ [\ d + \] \.name, productList \ [\ d + \] \ .description < param name =excludeNullProperties> true< / param> < / result> < / action> 我只想获取 productList 在Action类中 public class ProductDetailsAction extends ActionSupport { 私人长ID; 私人字符串barCode; // bardoce 私人字符串名称; private String desc; 私人长期数量; 私人浮动单位价格; 私人浮动卖价; private float totalSellingPrice; 私人长客户端ID; private ArrayList< BillDetails> BillList; 私人日期startDate; 私人日期endDate; private int discountPercentage; 私人时间戳addDate; private String dataStatus; private ArrayList< ProductDetailsAction>产品列表; private ArrayList< Product> productBillList; public String fetchByProductName(){ ProductDetailsBusiness pdb = new ProductDetailsBusiness(); Map data = pdb.fetchByProductName(getName()); setProductList((ArrayList)data.get(list)); 返回SUCCESS; } //其他方法和getter和setter } 我收到以下异常: SEVERE:处理请求期间发生异常:org.apache.struts2.json.JSONException:org。 apache.struts2.json.JSONException:org.hibernate.LazyInitializationException:无法初始化懒洋洋角色的集合:iland.hbm.Client.billDetailses,没有会议或会议是封闭的 org.apache.struts2.json.JSONException :org.apache.struts2.json.JSONException:org.apache.struts2.json.JSONException:org.hibernate.LazyInitializationException:无法初始化懒洋洋角色的集合:iland.hbm.Client.billDetailses,没有会话或会话关闭 at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246) at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178)在org.apache.struts2.json上的org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168)。 JSONWriter.value(JSONWriter.java:134) at org.apache.struts2.json.JSONWriter.write(JSONWriter.java:102) at org.apache.struts2.json.JSONUtil.serialize( JSONUtil.java:116) at org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:197) at org.apache.struts2.json.JSONResult.execute(JSONResult.java: 171) at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275) at org .apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2 .interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterIntercepto r.java:98)美元,com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246 B $ B)在com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)。在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)在com.opensymphony .xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor .intercept(我thodFilterInterceptor.java:98)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239 )在com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)。在COM .opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在org.apache.struts2.interceptor .MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor .java:91)。在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)在com.opensymphony .xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke (DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246 )在com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145 )在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246 )。在com.opensymphony.xwork2.interc eptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke( DefaultActionInvocation.java:246)美元,com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161 b $ b)在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)在org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) 在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)。在COM .opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor .ExceptionMappingInterceptor.intercept(ExceptionMappi ngInterceptor.java:189) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)在org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563) 在org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)$ b在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) $ b在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)。在组织.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorB ase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke( ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408 118))。在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:310)。在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)在java.lang中.Thr ead.run(Thread.java:744)引起:org.apache.struts2.json.JSONException:org.apache.struts2.json.JSONException:org.hibernate.LazyInitializationException:未能延迟初始化一个集合角色:iland.hbm.Client.billDetailses,没有会话或会话被关闭在org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246)在org.apache.struts2。 json.JSONWriter.processCustom(JSONWriter.java:178)处org.apache.struts2.json.JSONWriter org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168)。值(JSONWriter.java:134)在org.apache.struts2.json.JSONWriter.add(JSONWriter.java:390)在org.apache.struts2.json.JSONWriter.bean(JSONWriter。 java:231) ... 71 more 引起:org.apache.struts2.json.JSONException:org.hibernate.LazyInitializationException:无法懒惰地初始化一个角色集合:iland.hbm.Client .billDetailses,没有会话或会话被关闭在org.apache.struts2.json.JSO NWriter.bean(JSONWriter.java:246) at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178) at org.apache.struts2.json.JSONWriter.process( JSONWriter.java:168) at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134) at org.apache.struts2.json.JSONWriter.add(JSONWriter.java: 390) at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:231) ... 76 more 引起:org.hibernate.LazyInitializationException:无法延迟初始化角色的集合:iland.hbm.Client.billDetailses,没有会议或会议于在org.hibernate作为org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)封闭。 collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollectio at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111) at org.hibernate.collection.PersistentSet.equals(PersistentSet.java:429)$ b $ java.util.Vector.indexOf(Vector.java:408)$ b $ java.util.Vector.contains(Vector.java:367) at org.apache.struts2.json.JSONWriter .value(JSONWriter.java:117) at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:390) at org.apache.struts2.json.JSONWriter.bean(JSONWriter .java:231) ... 81 more 编辑: fetchByProductName(getName()); 方法只包含Jdbc代码。那为什么Hibernate Errorrs? 我从数据库获取日期并在控制台上打印。在成功之前,我给了 System.out.println(在行动); 这是正确的打印 It没有必要从数据库访问数据,那么为什么未能懒惰地初始化一个XXX集合 如何解决这个问题。 编辑 public class ProductDetailsBusiness { ProductDetailsAction pda = null; PreparedStatement ps = null; ResultSet rs = null,res = null; 连接con = null; String status = null; ArrayList< ProductDetailsAction> ar = null; $ b $ public Map fetchByProductName(String name,long cid){ System.out.println(ProductBusiness fetchByProductName()); status =成功; System.out.println(Name is+ name); ar = new ArrayList(); con = ConnectionFactory.getConnection(); 尝试{ String query =select * from product +where status =?and cid =? +order by add_date; ps = con.prepareStatement(query); ps.setString(1,active); ps.setLong(2,cid); System.out.println(执行前); res = ps.executeQuery(); System.out.println(Details are); while(res.next()){ pda = new ProductDetailsAction(); long id1 = res.getLong(id); 字符串barCode = res.getString(barcode); String name1 = res.getString(name); String desc = res.getString(description); long quntity = res.getLong(quntity); float unit_price = res.getLong(unit_price); float selling_price = res.getLong(selling_price); int discount_percent = res.getInt(discount_percent); pda.setId(id1); pda.setBarCode(barCode); pda.setName(name1); pda.setDesc(desc); pda.setQuantity(quntity); pda.setUnitPrice(unit_price); pda.setSellingPrice(selling_price); pda.setDiscountPercentage(discount_percent); System.out.println(id+ id1 +name+ name1 ++ desc); ar.add(pda); } con.close(); } catch(SQLException s){ status =failure; System.out.println(SQL code does not execute。+ s); } finally { try { if(con!= null){ con.close(); } } catch(SQLException sqe){ status =failure; System.out.println(SQLException+ sqe); } } Map m = new HashMap(); m.put(状态,状态); m.put(list,ar); 返回m; } //其他方法} class请参阅 JSON插件,你在你的项目中使用。并且您已经知道要限制序列化程序使用的属性,您应该使用 json 结果的 includeProperties 。还应该检查 root 属性。如果未指定,则json结果默认使用值栈的 top 对象。如果您的操作是模型驱动的,那么模型会覆盖操作。因此,您在 includeProperties 中使用的正则表达式适用于模型对象,并且序列化程序 JSONWriter 使用 BeanInfo 来提取bean的属性。首先,检查这些属性是否存在 @JSON 注释,如果它包含 serialize = false ,则跳过它,否则包括在内。这样,您可以通过将注释放在不会序列化为JSON的属性上来控制序列化过程,例如 @OneToMany 属性等。然后如果它们不是空的,则检查includeProperties 或 excludeProperties 。这时你应该承认,如果你没有与指定模式相匹配的属性,那么它们将不会被序列化。因此,让我们转到 LazyInitializationException ,正如我所说的,如果您在模型层次结构中有 @JSON serialize = false ,因为默认情况下它是 true 。 @JSON(serialize = false) public Object getBillDetailses(){return billDetailses; } 您在操作中表示您的 productList 类,那么你应该配置结果 <! - Result config - > < result type =json> < param name =includeProperties> ^ productList \ [\ d + \] \.id, ^ productList \ [\ d + \] \.name, ^ productList \ [\\ \\ d + \] \ .description < / param> < param name =root> #action < / param> < / result> 请注意, ^ 表示开始表达式,所以只包含操作属性。 I am calling autocomplete from jsp url: "ProductByName".On response in console it is showingPOST http://localhost:8082/Store/SingleProduct 500 (Internal Server Error)In jsp $(".name").autocomplete({ source: function(request, response) { $.ajax({ url: "ProductByName", dataType: "json", data: { str: request.term, maxRows: 5 },...In struts.xml<action name="ProductByName" class="iland.productdetails.ProductDetailsAction" method="fetchByProductName"> <result type="json"> <param name="includeProperties">productList\[\d+\]\.id, productList\[\d+\]\.name, productList\[\d+\]\.description </param> <param name="excludeNullProperties">true</param> </result> </action>I want to fetch only few properties of productListIn Action classpublic class ProductDetailsAction extends ActionSupport {private long id;private String barCode;//bardoceprivate String name;private String desc;private long quantity;private float unitPrice;private float sellingPrice;private float totalSellingPrice;private long clientId;private ArrayList<BillDetails> BillList;private Date startDate;private Date endDate;private int discountPercentage;private Timestamp addDate;private String dataStatus;private ArrayList<ProductDetailsAction> productList;private ArrayList<Product> productBillList;public String fetchByProductName() { ProductDetailsBusiness pdb = new ProductDetailsBusiness(); Map data = pdb.fetchByProductName(getName()); setProductList((ArrayList) data.get("list")); return SUCCESS; } //other methods and getter and setter }I am getting following exceptionSEVERE: Exception occurred during processing request: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: iland.hbm.Client.billDetailses, no session or session was closedorg.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: iland.hbm.Client.billDetailses, no session or session was closed at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246) at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178) at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168) at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134) at org.apache.struts2.json.JSONWriter.write(JSONWriter.java:102) at org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:116) at org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:197) at org.apache.struts2.json.JSONResult.execute(JSONResult.java:171) at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:744)Caused by: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: iland.hbm.Client.billDetailses, no session or session was closed at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246) at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178) at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168) at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134) at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:390) at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:231) ... 71 moreCaused by: org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: iland.hbm.Client.billDetailses, no session or session was closed at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246) at org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178) at org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168) at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134) at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:390) at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:231) ... 76 moreCaused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: iland.hbm.Client.billDetailses, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383) at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111) at org.hibernate.collection.PersistentSet.equals(PersistentSet.java:429) at java.util.Vector.indexOf(Vector.java:408) at java.util.Vector.contains(Vector.java:367) at org.apache.struts2.json.JSONWriter.value(JSONWriter.java:117) at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:390) at org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:231) ... 81 moreEdit:fetchByProductName(getName()); method contains only Jdbc code. Then why Hibernate Errorrs??I am fetching date from database and also printing on console. before success I gave System.out.println(In action); this is aslo printing properlyIt doesnt have to access data from database then why failed to lazily initialize a collection of XXXHow to resolve this.Edit public class ProductDetailsBusiness {ProductDetailsAction pda = null;PreparedStatement ps = null;ResultSet rs = null, res = null;Connection con = null;String status = null;ArrayList<ProductDetailsAction> ar = null;public Map fetchByProductName(String name, long cid) { System.out.println(" ProductBusiness fetchByProductName()"); status = "success"; System.out.println("Name is " + name); ar = new ArrayList(); con = ConnectionFactory.getConnection(); try { String query = "select * from product " + "where status=? and cid=? " + "order by add_date"; ps = con.prepareStatement(query); ps.setString(1, "active"); ps.setLong(2, cid); System.out.println("Before execution"); res = ps.executeQuery(); System.out.println("Details are"); while (res.next()) { pda = new ProductDetailsAction(); long id1 = res.getLong("id"); String barCode = res.getString("barcode"); String name1 = res.getString("name"); String desc = res.getString("description"); long quntity = res.getLong("quntity"); float unit_price = res.getLong("unit_price"); float selling_price = res.getLong("selling_price"); int discount_percent = res.getInt("discount_percent"); pda.setId(id1); pda.setBarCode(barCode); pda.setName(name1); pda.setDesc(desc); pda.setQuantity(quntity); pda.setUnitPrice(unit_price); pda.setSellingPrice(selling_price); pda.setDiscountPercentage(discount_percent); System.out.println("id "+id1+" name "+name1+" "+desc); ar.add(pda); } con.close(); } catch (SQLException s) { status = "failure"; System.out.println("SQL code does not execute." + s); } finally { try { if (con != null) { con.close(); } } catch (SQLException sqe) { status = "failure"; System.out.println("SQLException " + sqe); } } Map m = new HashMap(); m.put("status", status); m.put("list", ar); return m; } //other methods }Also updated action class Please also see it 解决方案 The serialization process to JSON is controlled by JSON result, which is a part of the JSON plugin that you used in your project. And you already know that to limit properties used by serializer you should use includeProperties of the json result. There's also root property that you should examine. If not specified the json result uses by default the top object of the value stack. If your action is model driven then model overrides action. So, the regexes you used in the includeProperties apply to the model object, and serializer which is JSONWriter uses BeanInfo to extract properties of the bean. First, these properties are checked for existence of @JSON annotation and if it has serialize=false, then it's skipped, otherwise it's included. This way you can control the serialization process via putting annotations on the properties that you won't serialize to JSON such as @OneToMany properties, etc. Then includeProperties or excludeProperties are checked if they are not empty. At this time you should acknowledge that if you don't have properties that matches to the patterns specified then them won't be serialized. So, lets move to LazyInitializationException, as I said if you have @JSON annotation ever in the model hierarchy you should put serialize=false inside it because by default it's true.@JSON(serialize=false)public Object getBillDetailses(){ return billDetailses; }You said your productList in the action class, then you should configure the result<!-- Result config --><result type="json"> <param name="includeProperties"> ^productList\[\d+\]\.id, ^productList\[\d+\]\.name, ^productList\[\d+\]\.description </param> <param name="root"> #action </param></result>Note, that ^ stands for beginning the expression, so only action properties will be included. 这篇关于org.apache.struts2.json.JSONException:org.hibernate.LazyInitializationException:未能延迟初始化集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-25 03:09