Results 1 to 5 of 5

Thread: hibernate.LazyInitializationException

  1. #1

    Default hibernate.LazyInitializationException

    Hi,

    I'm having trouble with a LIE, I guess it's a matter of configuring a detail, but I can't see the light...

    I obtain a list of object from the database into a dataTable. When I choose one register, I pass the chosen object to a form, to be edited.
    One of the fields is a List of objects (ManyToMany), which is mapped to a JSF selectManyCheckbox component. I have the converter in place for the List of objects.
    Now when I save, I get the following error:

    Code:
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection, 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.write(AbstractPersistentCollection.java:208)
    	at org.hibernate.collection.PersistentBag.add(PersistentBag.java:297)
    	at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel(MenuRenderer.java:378)
    	at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValue(MenuRenderer.java:125)
    	at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:311)
    	at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1023)
    The flow in which the view resides has <persistence-context />
    defined, and the DAO that obtained the list of objects has @Transactional enabled. I also defined the Field as @ManyToMany(fetch = FetchType.EAGER) so that all information gets loaded when the record is retrieved.
    The error is thrown when the List is actually empty, and a checked object is to be added to it.

    Could anybody tell what's missing here?

    Cheers Bo

    Web Flow 2.2.1, JSF, Hibernate 3.5.6

  2. #2
    Join Date
    Mar 2009
    Location
    Ajaccio, France
    Posts
    25

    Default

    Hello,

    Are there any subflow involved ?
    If yes, this could be helpfull:
    https://jira.springframework.org/browse/SWF-360

    Nicolas.

  3. #3

    Default

    No, no subflows involved...

  4. #4
    Join Date
    Mar 2010
    Location
    Fairfax, VA
    Posts
    2

    Default

    I had a similar problem, maybe this will help. I used the booking-faces example to build an application, and I noticed in some cases, <persistence-context/> worked as expected, and in other cases, it did not.

    It turned out that all of the LazyDataModel instances, which I initialized like this:

    Code:
    <persistence-context/>
    <var name="searchCriteria" class="org.springframework.webflow.samples.booking.SearchCriteria" />
    ....
    <on-entry>
       <set name="flowScope.hotels" value="searchCriteria.getDataModel(bookingService)" />
    </on-entry>
    Had a different entity manager , which did not appear to be persistent across the flow, than other elements in the flow using the same bookingService.

    I ended up doing it this way and it resolved the problem:

    Code:
    // I added these lines to HotelLazyDataModel:
    public HotelLazyDataModel(){}
    // Recall bookingService is annotated with @Service("bookingService")
    // and has a @PersistenceContext annotation to define the EntityManager
    @Autowired
    public void setBookingService(BookingService service)
    {
        bookingService = service;
    }
    public void setSearchCriteria(SearchCriteria criteria){...}
    
    // and the flow was changed like this:
    <var name="searchCriteria" class="org.springframework.webflow.samples.booking.SearchCriteria" />
    <var name="hotelDataModel" value="org.springframework.webflow.samples.booking.HotelLazyDataModel"/>
    ...
    <on-entry>
     <evaluate expression="hotelDataModel.setSearchCriteria(searchCriteria)"/>
    ...
    I got the idea for the solution b/c I had an action class that had an @Autowired annotation to set the bookingService, and the action class seemed to be using the same (persistent) EntityManager as other elements of the flow
    I tested it like this:
    Code:
    <set name="flowScope.testHotel" value="bookingService.findHotel(0)">
    <evaluate expression="myAction.checkHotelInEntityManager(testHotel)/>

  5. #5

    Default

    I've some more information:

    When I have
    Code:
    <h:selectManyCheckbox id="formIdioma"
    					value="#{searchCriteria.idiomas}" converter="#{idiomaConverter}"
    					layout="lineDirection">
    					<f:selectItems value="#{formReferenceData.allIdiomas}" />
    				</h:selectManyCheckbox>
    and searchCriteria.idiomas is not filled with dtabase data initially, the code works perfectly; searchCriteria.idiomas gets updated with the chosen fields.
    However, when it gets filled with the data already in the database (even if it's an empty collection) I get the LIE

    So somehow I guess the collection should be detached, but it's not quit clean to me why that is, I thought <persistence-context/> took care of this.
    Last edited by boudewijn; Dec 9th, 2010 at 11:07 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •