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.