Announcement Announcement Module
No announcement yet.
hibernate.LazyInitializationException Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • hibernate.LazyInitializationException


    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:

    org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(
    	at org.hibernate.collection.AbstractPersistentCollection.initialize(
    	at org.hibernate.collection.AbstractPersistentCollection.write(
    	at org.hibernate.collection.PersistentBag.add(
    	at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel(
    	at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValue(
    	at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(
    	at javax.faces.component.UIInput.getConvertedValue(
    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

    Are there any subflow involved ?
    If yes, this could be helpfull:



    • #3
      No, no subflows involved...


      • #4
        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:

        <var name="searchCriteria" class="" />
           <set name="" value="searchCriteria.getDataModel(bookingService)" />
        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:

        // I added these lines to HotelLazyDataModel:
        public HotelLazyDataModel(){}
        // Recall bookingService is annotated with @Service("bookingService")
        // and has a @PersistenceContext annotation to define the EntityManager
        public void setBookingService(BookingService service)
            bookingService = service;
        public void setSearchCriteria(SearchCriteria criteria){...}
        // and the flow was changed like this:
        <var name="searchCriteria" class="" />
        <var name="hotelDataModel" value=""/>
         <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:
        <set name="flowScope.testHotel" value="bookingService.findHotel(0)">
        <evaluate expression="myAction.checkHotelInEntityManager(testHotel)/>


        • #5
          I've some more information:

          When I have
          <h:selectManyCheckbox id="formIdioma"
          					value="#{searchCriteria.idiomas}" converter="#{idiomaConverter}"
          					<f:selectItems value="#{formReferenceData.allIdiomas}" />
          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, 11:07 AM.