I have been using hibernate fro almost two years with great success. I use Spring's OpenSessionInView pattern and all works great. Now, all of a sudden, I am getting LazyInitializationExceptions on a particular query.
I am querying for a list of Applications.
Applications belong to a CommonApplicationForm. Each CommonApplicationForm has one or more applications.
CommonApplicationForm has several other collections associated with it. All collections are lazy.
When I try to lazily load a collection in CommonApplicationForm, I get the exception, and I've traced it to the
session.getPersistenceContext().containsCollection(this)
check in AbstractPersistentCollection (line 176). (the session is open and it is not null)
What's even worse is that most of the time, it initializes these collections just fine. Actually if I restrict the result size to one record (one application), this error never occurs. But certain applications cause this error. And I can't make out a rhyme or reason about which applications cause this exception to occur.
I've read everywhere I could, without much success.
Hibernate version: 3.1.2
Mapping documents:
Applications
Code:
<hibernate-mapping>
<class name="org.oslerproject.ram.model.impl.ApplicationImpl" table="applications"><id unsaved-value="null" name="id">
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>
</id><timestamp unsaved-value="null" name="updated"/><property name="matched" unique="false" update="true" insert="true" optimistic-lock="true" lazy="false"/><property name="rank" unique="false" update="true" insert="true" optimistic-lock="true" lazy="false"/><one-to-one name="completeness" cascade="all" constrained="false" class="org.oslerproject.ram.model.impl.ApplicationCompletenessStateImpl" embed-xml="true"/><many-to-one column="interview_visit_id" name="interviewVisit" class="org.oslerproject.ram.model.impl.VisitImpl" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true"/><many-to-one cascade="none" name="targetOrganization" class="org.oslerproject.institution.model.Organization" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="organization_id" index="organization_idx"/>
</many-to-one><many-to-one name="commonApplicationForm" class="org.oslerproject.ram.model.impl.CAFImpl" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="caf_id" index="caf_idx"/>
</many-to-one><property name="uid" lazy="false" not-null="true" length="50" unique="true" update="true" insert="true" optimistic-lock="true"/><property name="created" unique="false" update="true" insert="true" optimistic-lock="true" lazy="false"/><filter condition="exists (select caf.id from caf where caf.year=:year and caf.id=caf_id)" name="yearFilter"/><filter condition="organization_id in (:organizations)" name="organizationFilter"/></class>
</hibernate-mapping>
CommonApplicationForm:
Code:
<hibernate-mapping>
<class name="org.oslerproject.ram.model.impl.CAFImpl" table="caf"><id unsaved-value="null" name="id">
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>
</id><timestamp unsaved-value="null" name="updated"/><set table="ram_reviews" lazy="true" cascade="all-delete-orphan" order-by="id" name="reviews" sort="unsorted" inverse="false" optimistic-lock="true" embed-xml="true">
<key column="caf_id" on-delete="noaction"/>
<one-to-many class="org.oslerproject.ram.model.impl.CAFReviewImpl" not-found="exception" embed-xml="true"/>
</set><set table="ram_annotations" lazy="true" cascade="all-delete-orphan" order-by="id" name="annotations" sort="unsorted" inverse="false" optimistic-lock="true" embed-xml="true">
<key column="caf_id" on-delete="noaction"/>
<one-to-many class="org.oslerproject.ram.model.impl.AnnotationImpl" not-found="exception" embed-xml="true"/>
</set><set table="visits" lazy="true" cascade="all-delete-orphan" order-by="id" name="visits" sort="unsorted" inverse="false" optimistic-lock="true" embed-xml="true">
<key column="caf_id" on-delete="noaction"/>
<one-to-many class="org.oslerproject.ram.model.impl.VisitImpl" not-found="exception" embed-xml="true"/>
</set><set table="ram_correspondence" lazy="true" cascade="all-delete-orphan" order-by="id" name="correspondence" sort="unsorted" inverse="false" optimistic-lock="true" embed-xml="true">
<key column="caf_id" on-delete="noaction"/>
<one-to-many class="org.oslerproject.ram.model.impl.CorrespondenceImpl" not-found="exception" embed-xml="true"/>
</set><one-to-one name="uploadStatus" cascade="all" constrained="false" class="org.oslerproject.ram.model.impl.CAFUploadStatusImpl" embed-xml="true"/><property name="dateReceived" column="received" unique="false" update="true" insert="true" optimistic-lock="true" lazy="false"/><many-to-one not-null="true" property-ref="year" name="targetAcademicYear" class="org.oslerproject.institution.model.AcademicYear" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="year" index="year_idx"/>
</many-to-one><set table="applications" lazy="true" cascade="all-delete-orphan" order-by="id" name="applications" sort="unsorted" inverse="false" optimistic-lock="true" embed-xml="true">
<key column="caf_id" on-delete="noaction"/>
<one-to-many class="org.oslerproject.ram.model.impl.ApplicationImpl" not-found="exception" embed-xml="true"/>
</set><set table="caf_documents" lazy="true" cascade="all-delete-orphan" order-by="id" name="documents" sort="unsorted" inverse="false" optimistic-lock="true" embed-xml="true">
<key column="caf_id" on-delete="noaction"/>
<one-to-many class="org.oslerproject.ram.model.impl.CAFDocumentImpl" not-found="exception" embed-xml="true"/>
</set><component name="immigrationData" class="org.oslerproject.ram.model.impl.ImmigrationDataImpl" unique="false" update="true" insert="true" lazy="false" optimistic-lock="true">
<property name="anticipatedStartDate" column="anticipated_start" unique="false" update="true" insert="true" optimistic-lock="true" lazy="false"/>
<many-to-one column="citizenship_id" name="citizenship" class="org.oslerproject.ram.model.Citizenship" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true"/>
<many-to-one column="visa_id" cascade="save-update" name="currentVisa" class="org.oslerproject.ram.model.impl.VisaImpl" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true"/>
<many-to-one column="desired_visa_id" name="desiredTrainingVisa" class="org.oslerproject.ram.model.VisaType" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true"/>
<many-to-one column="ecfmg_id" name="ecfmgCertification" class="org.oslerproject.ram.model.impl.CAFDocumentImpl" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true"/>
<property name="immigrationIssues" column="immigration_issues" unique="false" update="true" insert="true" optimistic-lock="true" lazy="false"/>
</component><many-to-one column="portfolio_id" cascade="save-update" name="portfolio" class="org.oslerproject.ram.model.impl.ApplicationPortfolioImpl" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true"/><map table="caf_responses" lazy="true" cascade="all-delete-orphan" order-by="id" name="responses" inverse="false" sort="unsorted" optimistic-lock="true" embed-xml="true">
<key column="caf_id" on-delete="noaction"/>
<index-many-to-many column="question_id" class="org.oslerproject.ram.model.CAFQuestion"/>
<one-to-many class="org.oslerproject.ram.model.impl.CAFResponseImpl" not-found="exception" embed-xml="true"/>
</map><many-to-one name="person" class="org.oslerproject.people.model.impl.PersonImpl" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="person_id" index="person_idx"/>
</many-to-one><property name="uid" lazy="false" not-null="true" length="50" unique="true" update="true" insert="true" optimistic-lock="true"/><property name="created" unique="false" update="true" insert="true" optimistic-lock="true" lazy="false"/><filter condition=":year = year" name="yearFilter"/><filter condition="exists (select applications.caf_id from applications where applications.organization_id in (:organizations) and applications.id=id)" name="organizationFilter"/></class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.oslerproject.ram.model.impl.CAFImpl.reviews, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
at org.codehaus.groovy.runtime.Invoker.asIterator(Invoker.java:433)
at org.codehaus.groovy.runtime.InvokerHelper.asIterator(InvokerHelper.java:143)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.collect(DefaultGroovyMethods.java:690)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.collect(DefaultGroovyMethods.java:677)
at gjdk.java.lang.Object_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:110)
at org.codehaus.groovy.runtime.NewInstanceMetaMethod.invoke(NewInstanceMetaMethod.java:99)
at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:1455)
at groovy.lang.MetaClass.invokeMethod(MetaClass.java:318)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:145)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:104)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod(ScriptBytecodeAdapter.java:85)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodSafe(ScriptBytecodeAdapter.java:92)
at org.oslerproject.ram.properties.ReviewsCollection.doExecute(Unknown Source)
at org.oslerproject.ram.properties.CafJavaPropertyScript.execute(CafJavaPropertyScript.java:25)
at org.oslerproject.framework.web.property.JavaCollectionBean.doGetAsList(JavaCollectionBean.java:28)
at org.oslerproject.framework.web.property.AbstractCollectionBean.getAsList(AbstractCollectionBean.java:116)
at org.oslerproject.framework.web.property.AbstractCollectionBean.getAsString(AbstractCollectionBean.java:54)
at org.oslerproject.framework.web.property.CompositePropertyBean.doGetAsActualType(CompositePropertyBean.java:53)
at org.oslerproject.framework.web.property.impl.AbstractPropertyBean.doGetAsString(AbstractPropertyBean.java:172)
at org.oslerproject.framework.web.property.impl.AbstractPropertyBean.getAsString(AbstractPropertyBean.java:133)
at org.oslerproject.ram.web.beans.view.RankView.getColumnValue(RankView.java:178)
at org.oslerproject.ram.web.beans.view.RankView$$FastClassByCGLIB$$ccaacb85.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
at org.oslerproject.ram.web.beans.view.RankView$$EnhancerByCGLIB$$e55f54d0.getColumnValue(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:431)
at org.apache.myfaces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:82)
at com.sun.facelets.el.LegacyELContext$LegacyELResolver.getValue(LegacyELContext.java:141)
at com.sun.el.parser.AstValue.getValue(AstValue.java:96)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:183)
at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
at com.sun.facelets.el.LegacyValueBinding.getValue(LegacyValueBinding.java:56)
at javax.faces.component.UIOutput.getValue(UIOutput.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.sun.facelets.util.DevTools.writeAttributes(DevTools.java:240)
at com.sun.facelets.util.DevTools.writeStart(DevTools.java:277)
at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:189)
at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:207)
at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:207)
at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:207)
at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:207)
at com.sun.facelets.util.DevTools.debugHtml(DevTools.java:107)
at com.sun.facelets.FaceletViewHandler.handleRenderException(FaceletViewHandler.java:511)
at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:489)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:372)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at de.mindmatters.faces.spring.support.RequestHandledFilter.doFilter(RequestHandledFilter.java:150)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.myfaces.component.html.util.ExtensionsFilter.doFilter(ExtensionsFilter.java:123)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:292)
at net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:84)
at net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter.doFilter(SecurityEnforcementFilter.java:182)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.wrapper.ContextHolderAwareRequestFilter.doFilter(ContextHolderAwareRequestFilter.java:50)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:226)
at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
at net.sf.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:173)
at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at [b]org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)[/b]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:534)
Name and version of the database you are using:The generated SQL (show_sql=true):Debug level Hibernate log excerpt:Code:
Code:
Code: