-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: LazyInitializationException ManyToMany Relation CRUD
PostPosted: Wed Dec 22, 2010 12:45 pm 
Newbie

Joined: Wed Dec 22, 2010 11:51 am
Posts: 5
Hi,

in our application we are using 2 entities AppUser and AppGroup wich are related in a ManyToMany Relation. We integrated different views for the CRUD functionality for each entity.

First, the List where all entities of one type are depicted. After selection of one entity each attribute is shown in a second view, and by using the "edit" button the third view is displayed, where the user can edit the selected entity. In this last view the user should be able to use the "update" button for updating this entity. Due to the fact, that we are using a ManyToMany relation between AppUser and AppGroup in the edit-view of one AppUser-entity all AppGroups are shown (for selecting).

By using the "update" button in the AppUserEdit view we are getting a org.hibernate.LazyInitializationException.

Here is the used code for the Entity AppGroup:

Code:
@Entity
@Table(name = "app_group")
public class AppGroup extends BaseEntity implements Serializable
{
  private String name;
  private Set<AppUser> users;
 
// ...
// Constructors (no Annotation)
// ...

  /* (non-Javadoc)
   * @see at.icbi.seqbench.entity.base.BaseEntity#getId()
   */
  @Id
  @Column(name = "id",nullable = false)
  @GeneratedValue(strategy = GenerationType.AUTO,generator = "group_seq_gen")
  @SequenceGenerator(name = "group_seq_gen",sequenceName = "group_seq")
  @Override
  public Long getId()
  {
    return this.id;
  }
   
  public void setUsers(Set<AppUser> users)
  {
    this.users = users;
  }

  @ManyToMany(cascade ={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
  @JoinTable(name="AppUser_AppGroup",
          joinColumns={@JoinColumn(name="group_id")},
          inverseJoinColumns={@JoinColumn(name="user_id")})
  public Set<AppUser> getUsers()
  {
    return users;
  }
 
// ...
// further getters and setters
// ...
}


for AppUser:

Code:
@Entity
@Table(name = "app_user")
@NamedQueries( { @NamedQuery(name = AppUser.findAppUserByExternalId,query = "Select a FROM AppUser a WHERE a.externalId=:externalId") })
public class AppUser extends BaseEntity implements Serializable

  //Named query
  public static final String findAppUserByExternalId = "findAppUserByExternalId";

  private String firstName;
  private String lastName;
  private Long externalId;
  private List<AppGroup> groups;

// ...
// Constructors (no Annotation)
// ...

  @Id
  @Column(name = "id",nullable = false)
  @GeneratedValue(strategy = GenerationType.AUTO,generator = "user_seq_gen")
  @SequenceGenerator(name = "user_seq_gen",sequenceName = "user_seq")
  @Override
  public Long getId()
  {
    return this.id;
  }
 
  public void setGroups(List<AppGroup> groups)
  {
    this.groups = groups;
  }
 
  @ManyToMany(cascade ={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch=FetchType.EAGER)
  @JoinTable(name="AppUser_AppGroup",
          joinColumns={@JoinColumn(name="user_id")},
          inverseJoinColumns={@JoinColumn(name="group_id")})
  public List<AppGroup> getGroups()
  {
    return groups;
  }

// ...
// further getters and setters
// ...
}


and for the UserEdit.xhtml:

Code:
...
<h:commandButton id="update"
   value="#{messages.update}"
   action="#{appUserHome.update}"
   rendered="#{appUserHome.managed}" />
...


But here the method appUserHome.update() is never called. I am getting the LazyInitializationException "failed to lazily initialize a collection, no session or session was closed" before.
We are using conversation scope.

Can you help us? We're not making any headway.


Top
 Profile  
 
 Post subject: Re: LazyInitializationException ManyToMany Relation CRUD
PostPosted: Wed Dec 22, 2010 3:58 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
How are your database sessions managed? If you load an object from the database that has a lazy-loaded collection and the Hibernate session is closed (for whatever reason), you need to either:

1) Load the object AGAIN in your view, with a new Hibernate session;
or 2) Attach the original object to the new Hibernate session.

Without knowing the whole lifecycle of a request I can only guess at what your setup is. But essentially it looks like you're accessing a lazy-loaded collection in one of your entities (looks like it probably happens before the <h:commandButton ... /> lines are even reached) but the Hibernate session that originally managed the loaded object has been closed so the collection can't be initialized then in your view.


Top
 Profile  
 
 Post subject: Re: LazyInitializationException ManyToMany Relation CRUD
PostPosted: Thu Dec 23, 2010 5:06 am 
Newbie

Joined: Wed Dec 22, 2010 11:51 am
Posts: 5
Hi silvaran,

I'm usign an EAGER loading. But I forgot to publish an other part of my code!
In my view I disply all entities of AppGroup, because they should be available for selecting...

Here is the code of my AppUserEdit.xhtml:

Code:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:rich="http://richfaces.org/rich"
   xmlns:csv="http://richfaces.org/csv"
   xmlns:a4j="http://richfaces.org/a4j" template="/layout/template.xhtml">

   <f:metadata><f:viewParam name="userId" value="#{appUserHome.id}" /></f:metadata>

   <ui:define name="body">
      <h:form id="user" styleClass="edit">
         <rich:panel>
            <ui:decorate template="/layout/edit.xhtml">
               <ui:define name="label">#{messages.firstName}</ui:define>
               <ui:param name="inputid" value="firstname"/>
               <ui:param name="required" value="true"/>
               <h:inputText id="firstname" styleClass="inputEdit" value="#{appUserHome.instance.firstName}">
                  <csv:beanValidator/>
               </h:inputText>
            </ui:decorate>

            <ui:decorate template="/layout/edit.xhtml">
               <ui:define name="label">#{messages.lastName}</ui:define>
               <ui:param name="inputid" value="lastname"/>
               <ui:param name="required" value="true"/>
               <h:inputText id="lastname" styleClass="inputEdit" value="#{appUserHome.instance.lastName}">
                  <csv:beanValidator/>
               </h:inputText>
            </ui:decorate>

            <ui:decorate template="/layout/edit.xhtml">
               <ui:define name="label">#{messages.groups}</ui:define>
               <h:selectManyListbox id="groups" converter="baseEntityConverter" value="#{appUserHome.instance.groups}" size="3">
                  <f:selectItems value="#{appGroupList.all}" var="group"
                        itemLabel="#{group.name}" itemValue="#{group}" />
               </h:selectManyListbox>
            </ui:decorate>

            <div style="clear: both" />
         </rich:panel>

         <div class="actionButtons">
            <h:commandButton id="update"
               value="#{messages.update}"
               action="#{appUserHome.update}" />

            <h:button id="cancelEdit"
               value="#{messages.cancel}"
               outcome="/user/user" />
         </div>
      </h:form>
   </ui:define>
</ui:composition>


Top
 Profile  
 
 Post subject: Re: LazyInitializationException ManyToMany Relation CRUD
PostPosted: Tue Jan 11, 2011 11:32 am 
Newbie

Joined: Wed Dec 22, 2010 11:51 am
Posts: 5
know I found that the problem is the tag <h:selectManyListbox ...>.
does anyone know an other component for selecting multiple entries?


Top
 Profile  
 
 Post subject: Re: LazyInitializationException ManyToMany Relation CRUD
PostPosted: Thu Jan 20, 2011 5:31 am 
Newbie

Joined: Wed Dec 22, 2010 11:51 am
Posts: 5
Hi,
the solution is in the h:selectManyListbox tag.... I added the collectionType and it works!

Code:
                    <h:selectManyListbox id="groups"
                        collectionType="java.util.ArrayList"
                        converter="baseEntityConverter"
                        value="#{appUserHome.instance.groups}" size="3">
                       <f:selectItems value="#{appGroupList.all}"
                               var="group"
                               itemLabel="#{group.name}" itemValue="#{group}" />
                    </h:selectManyListbox>       


Top
 Profile  
 
 Post subject: Re: LazyInitializationException ManyToMany Relation CRUD
PostPosted: Thu Dec 11, 2014 2:54 am 
Newbie

Joined: Thu Dec 11, 2014 2:47 am
Posts: 1
your answer is "easy" and works very fine for me. im using primefaces with an p:selectManyMenu and i get the error... lazyInitializationException when i try "update" an object with JPA with an relation ManyToMany.
Many Thanks!, because im trying with fetch type eager, named queries with fetch join, etc... and no results, but only changing this p:selectManyMenu for h:selectManyListbox with collectionType="java.util.ArrayList" all my problems dessapear.

Infinite Thanks.!!! :D :D :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.