Hibernate LazyInitializationException And p:selectCheckboxMenu

I had a simple Entity with a @oneToMany relationship.

Let’s call the Entity “Store” and the Entity of the relationship “Product”, obviously a Store has many Products.
When it comes to use selectCheckboxMenu with the input (update) of products, a bug arise “org.hibernate.LazyInitializationException: could not initialize proxy – no Session”.
For some reason using the primefaces selectCheckboxMenu (or h:selectmanycheckbox of the slandered JSF components) would end the Session of the current transaction.

to solve this issue I had to supply the type of my collection to the component with attribute name=”collectionType”.

<p:selectCheckboxMenu > 	 	 
 <f:attribute name="collectionType" value="java.util.ArrayList" />	 	 
 <f:selectItems value="#{bean.getStoreProducts()}" 	 	 
 var="product" itemLabel="#{product.name}"	 	 
 itemValue="#{product}" />	 	 

it depends on your type of List if it’s a Set for example you can use java.util.HashSet instead of java.util.ArrayList.

If you think something is wrong please feel free to comment :)
  1. Karl says:


    I’ve been struggling half a day with that error. I don’t use primefaces but still got that error.
    I’ve tried every LazyInitializationException-solution known to man (ViewFilters, Stateful beans, etc) evevry search said that it had to do with the Hibernate session.
    All I wanted was to update the list via a simple JSF-form and selectManyCheckBox.
    That little fixed everything!

  2. XtremeBiker says:

    Hi Hatem,

    First of all, congrats for your blog. It looks interesting.

    Related to this bug, it seems it has to do more with Mojarra rather than just Primefaces. Have a look to http://stackoverflow.com/questions/8802854/lazyinitializationexception-with-cdi-managed-bean-and-stateful-session-bean and http://stackoverflow.com/questions/14028373/hibernate-replaced-persistentbag-with-arraylist-is-returned-back-again-in-the-sa

    Just keep on the good work!

    • Hatem Alimam says:

      I imagined so, honestly I remember that this problem toke me a while to solve.

      Anyway thanks for the comment, I’ll update the post.

  3. Maik says:

    Holy smokes!
    I had that error.
    I was trying to figure out what caused it for Two and a half days!
    It almost drove me crazy. I couldn’t find it. It doesn’t make any sense to me.
    Finally i found your solution. Thanks a ton!!!!

  4. Thanks friend! You saved me!

  5. Very simple tip and userfull. Works fine to my case.



Leave a reply.