我有一个应用程序,我正在扩展以提供REST API。在主站点中一切正常,但是当我尝试点击REST API时,我在异常日志中得到以下内容:
"Error","ajp-bio-8014-exec-3","12/02/14","12:54:06","table","failed to lazily initialize a collection of role: field, could not initialize proxy - no Session The specific sequence of files included or processed is: service.cfc'' "
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: field, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:137)
at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:242)
at coldfusion.runtime.xml.ListIndexAccessor.getSize(ListIndexAccessor.java:44)
at coldfusion.runtime.xml.ArrayHandler.serialize(ArrayHandler.java:69)
at coldfusion.runtime.xml.CFComponentHandler.serialize(CFComponentHandler.java:106)
at coldfusion.runtime.XMLizerUtils.serializeXML(XMLizerUtils.java:83)
at coldfusion.rest.provider.CFObjectProvider.writeTo(CFObjectProvider.java:378)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1479)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
at coldfusion.rest.servlet.CFRestServletContainer.service(CFRestServletContainer.java:141)
at coldfusion.rest.servlet.CFRestServletContainer.service(CFRestServletContainer.java:86)
at coldfusion.rest.servlet.CFRestServlet.serviceUsingAlreadyInitializedContainers(CFRestServlet.java:556)
at coldfusion.rest.servlet.CFRestServlet.invoke(CFRestServlet.java:434)
at coldfusion.rest.servlet.RestFilter.invoke(RestFilter.java:58)
at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94)
at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.rest.servlet.CFRestServlet.invoke(CFRestServlet.java:409)
at coldfusion.rest.servlet.CFRestServlet.service(CFRestServlet.java:400)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:422)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:198)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
禁用延迟加载会解决此问题,但会导致无法接受的性能(加载时间从200ms到22s)。我不知道怎么办呢。
我是ColdFusion中的REST新手,在我看来,CFC的处理方式不同寻常。它们似乎没有初始化(init方法似乎没有运行),现在似乎ORM也没有处理相同。我错过了什么吗?
以下是产生此错误的代码的摘录:
component rest="true" restpath="item"
{
import model.beans.*;
remote item function getitem( numeric id restargsource="Path" ) restpath="{id}" httpmethod="GET"
{
var item = entityLoad("item",{ id = id },true);
return item;
}
}
豆子:
component persistent="true" table="item" output="false" extends="timestampedBean" batchsize="10" cacheuse="read-only"
{
/* properties */
property name="id" column="id" type="numeric" ormtype="int" fieldtype="id" generator="identity";
property name="title" column="title" type="string" ormtype="string";
property name="description" column="description" type="string" ormtype="string";
property name="status" column="status" type="numeric" ormtype="byte" default="0" ;
property name="user" fieldtype="many-to-one" cfc="user" fkcolumn="userid" inversejoincolum="userid" lazy="true" cacheuse="read-only";
property name="field" type="array" fieldtype="many-to-many" cfc="field" fkcolumn="id" linktable="items_fields" inversejoincolumn="fieldid" lazy="extra" batchsize="10" cacheuse="read-only";
}
我还注意到在stdout日志中Hibernate正在记录查询,但随后它记录了“No session”错误:
Hibernate:
select
item0_.id as id0_0_,
item0_.dtcreated as dtcreated0_0_,
item0_.dtmodified as dtmodified0_0_,
item0_.title as title0_0_,
item0_.description as descript6_0_0_,
item0_.status as status0_0_,
item0_.userid as userid0_0_
from
item item0_
where
item0_.id=?
Dec 2, 2014 15:23:00 PM Error [ajp-bio-8014-exec-3] - failed to lazily initialize a collection of role: field, could not initialize proxy - no Session The specific sequence of files included or processed is: service.cfc''
我可能还应该补充一点,这个“item”表是多对多关系的一部分,因此“role:field的集合”引用了外表。
答案 0 :(得分:2)
org.hibernate.LazyInitializationException是一个流行的hibernate问题。根本原因是您没有打开休眠会话,因为没有可用的事务。 有几种方法可以解决这个问题。 请阅读以下链接:
http://www.javacodegeeks.com/2012/07/four-solutions-to-lazyinitializationexc_05.html http://javarevisited.blogspot.ru/2014/04/orghibernatelazyinitializationException-Could-not-initialize-proxy-no-session-hibernate-java.html
或者您可以使用
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
。更多信息请访问 Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans
答案 1 :(得分:0)
将列表设置为null
示例:
迭代对象并将每个实体的列表设置为null
entity.setList(空);