LazyInitializationException in RESTORE_VIEW (improper PersistentSet instantiation)

932 Views Asked by At

I'm getting started to use IceFaces and had encountered a problem when trying drag'n'drop support.

IceFaces 2.0.0
JSF 2.0
Mojarra 2.0.2 (FCS b10) (JSF reference implementation)
Spring 3.0.3
Hibernate 3.5.1-Final
JPA 2.0 (with OpenEntityManagerInViewFilter)

The problem is LazyInitializationException while dropping. My first conclusion was an incorrect persistence context lifecycle management settings, but after some hours of playing with settings, dependencies and debugging I have found strange thing: JSF implementation is creating PersistentSet instance by reflection with default constructor (!) in RESTORE_VIEW phase and trying to add values to it. This action causes LazyInitializationException because PersistentSet is created as a simple collection like ArrayList and not configured properly (session not set).

org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException
                                        (AbstractPersistentCollection.java:380)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected
                                        (AbstractPersistentCollection.java:372)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
    at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212)
    at javax.faces.component.UIComponentBase.restoreAttachedState(UIComponentBase.java:1582)
    at javax.faces.component.ComponentStateHelper.restoreState(ComponentStateHelper.java:290)
    at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1444)
    at com.icesoft.faces.component.panelseries.UISeries.restoreState(UISeries.java:642)
    at com.icesoft.faces.component.panelseries.PanelSeries.restoreState(PanelSeries.java:138)
    at com.sun.faces.application.view.StateManagementStrategyImpl$2.visit(StateManagementStrategyImpl.java:231)
    at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:147)
    at com.icesoft.faces.component.panelseries.UISeries.visitTree(UISeries.java:814)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457)
    at javax.faces.component.UIForm.visitTree(UIForm.java:333)
    at com.icesoft.faces.component.panelseries.UISeries.visitColumnsAndRows(UISeries.java:898)
    at com.icesoft.faces.component.panelseries.UISeries.visitTree(UISeries.java:832)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457)
    at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:223)
    at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177)
    at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:131)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:430)
    at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:143)
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
javax.faces.FacesException: Unexpected error restoring state for component with id castle-list:j_idt12:castle-resident-list.  
Cause: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed.
    at com.sun.faces.application.view.StateManagementStrategyImpl$2.visit(StateManagementStrategyImpl.java:239)
    at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:147)
    at com.icesoft.faces.component.panelseries.UISeries.visitTree(UISeries.java:814)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457)
    at javax.faces.component.UIForm.visitTree(UIForm.java:333)
    at com.icesoft.faces.component.panelseries.UISeries.visitColumnsAndRows(UISeries.java:898)
    at com.icesoft.faces.component.panelseries.UISeries.visitTree(UISeries.java:832)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1457)
    at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:223)
    at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177)
    at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:131)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:430)
    at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:143)
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.LazyInitializationException: 
                    failed to lazily initialize a collection, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException
                                        (AbstractPersistentCollection.java:380)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected
                                        (AbstractPersistentCollection.java:372)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
    at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212)
    at javax.faces.component.UIComponentBase.restoreAttachedState(UIComponentBase.java:1582)
    at javax.faces.component.ComponentStateHelper.restoreState(ComponentStateHelper.java:290)
    at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1444)
    at com.icesoft.faces.component.panelseries.UISeries.restoreState(UISeries.java:642)
    at com.icesoft.faces.component.panelseries.PanelSeries.restoreState(PanelSeries.java:138)
    at com.sun.faces.application.view.StateManagementStrategyImpl$2.visit(StateManagementStrategyImpl.java:231)
    ... 34 more

This code of JSF implementation Library contains a problem:

(javax/faces/component/UIComponentBase.java:1566-1591):

if (stateObj instanceof List) {
    List<StateHolderSaver> stateList = (List<StateHolderSaver>) stateObj;
    Collection<Object> retCollection = null;
    StateHolderSaver collectionSaver = stateList.get(0);
    Class collectionClass = (Class) collectionSaver.restore(context);
    try {
        retCollection = (Collection<Object>) collectionClass.newInstance();     //(*)
    }
    catch (Exception e) {
        if (LOGGER.isLoggable(Level.SEVERE)) {
            LOGGER.log(Level.SEVERE, e.toString(), e);
        }
        throw new IllegalStateException("Unknown object type");
    }
    for (int i = 1, len = stateList.size(); i < len; i++) {
        try {
            retCollection.add(stateList.get(i).restore(context));       //(**)
        } catch (ClassCastException cce) {
            if (LOGGER.isLoggable(Level.SEVERE)) {
                LOGGER.log(Level.SEVERE, cce.toString(), cce);
            }
            throw new IllegalStateException("Unknown object type");
        }
    }
    result = retCollection;
}
  • * in my case "collectionClass" is "org.hibernate.collection.PersistentSet".
  • ** "retCollection.add" invocation results LazyInitializationException

Simple maven project can be downloaded here. just run it as web app, create few entries in "Heroes" and "Castles" section and then try to bind them by drag'n'drop in "Castles & Heroes" section.

It isn't clear, who is responsible for this issue - JSF RI, IceFaces or my hands=) I had post this problem on IceFaces and Oracle forums:

http://www.icefaces.org/JForum/posts/list/18539.page
http://forums.oracle.com/forums/thread.jspa?messageID=9328036

Advise me some workarounds or point to my errors, please=)

1

There are 1 best solutions below

0
On

I had a similar promblem with primefaces p:selectManyCheckbox component. Maybe not exactly your case, but adding f:attribute name="collectionType" to component solve the problem.

<p:selectManyCheckbox value="#{offerEditBean.selectedFloors}" converter="ait.entity.converter" layout="pageDirection">
    <f:attribute name="collectionType" value="java.util.HashSet" />
    <f:selectItems value="#{offerEditBean.floors}" var="item" itemValue="#{item}" itemLabel="#{item.name}" />
</p:selectManyCheckbox>

FYI: Instance variable selectedFloors initialized from entities @OneToMany Set property.

So, answering your question, it is mostly a JSF issue.

I found this solution here http://forum.primefaces.org/viewtopic.php?f=3&t=30765