我正在从 JSP url: "ProductByName"
调用自动完成功能。
在控制台响应时显示
POST http://localhost:8082/Store/SingleProduct 500 (Internal Server Error)
在 JSP 中:
$(".name").autocomplete({
source: function(request, response) {
$.ajax({
url: "ProductByName",
dataType: "json",
data: {
str: request.term,
maxRows: 5
},...
在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>
我只想获取productList
的几个属性
在操作类中:
public class ProductDetailsAction extends ActionSupport {
private long id;
private String barCode;//bardoce
private 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
}
我遇到以下异常:
SEVERE: 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 closed
org.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 more
Caused 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 more
Caused 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 more
编辑:
fetchByProductName(getName());
方法仅包含 Jdbc 代码。那么为什么 Hibernate 会出现错误呢?
我正在从数据库中获取日期并在控制台上打印。在成功之前,我给出了 System.out.println(
In action);
这也是正确打印的
它不必从数据库访问数据,那么为什么无法延迟初始化 XXX 集合
如何解决这个问题。
编辑:
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
}
还更新了操作类,另请参阅
最佳答案
LazyInitializationException
这是当 Struts 2 json 插件尝试序列化您的 productList
对象时引发此问题的根本原因。根据异常(exception)文档
(It) Indicates access to unfetched data outside of a session context. For example, when an uninitialized proxy or collection is accessed after the session was closed.
这意味着您正在尝试访问 billDetailses
实体的集合,该实体是从数据库以 java 中的 hibernate 代理形式加载的。根据 hibernate document
A LazyInitializationException will be thrown by Hibernate if an uninitialized collection or proxy is accessed outside of the scope of the Session, i.e., when the entity owning the collection or having the reference to the proxy is in the detached state.
因此这意味着 billDetailses
的 list
集合未初始化,您可以在完成操作之前强制初始化
使用
Hibernate.initialize(data) // collection entity
在集合映射上设置FetchType.EAGER
也可以解决问题,但它会增加billDetailses
对象的开销,因为每次在java中加载该对象时,它的集合都会强制加载,从我的角度来看,这就像购买法拉利并始终以一档运行。
如果您经常遇到LazyInitializationException
,我建议尝试使用 OpenSessionInViewFilter
这使您的 hibernate session 从请求开始一直保持打开状态,直到您的应用程序完成响应为止。
关于java - org.apache.struts2.json.JSONException : org. hibernate.LazyInitializationException:未能延迟初始化集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24301980/