[jboss-user] [EJB 3.0] - LazyInitializationException in many-to-many relation that co

JuliaF do-not-reply at jboss.com
Mon Apr 21 20:01:31 EDT 2008


I am not sure if I should be asking my question here or at the Beginners Corner. I am just starting with EJB3 and trying to build a simple test application.
I am getting a LazyInitializationException: failed to lazily initialize a collection of role: eco.ejb3.entity.Subject.usersThatSubscribedToMe, no session or session was closed when my JUnit test tries to exercise a method of my EJB3 session bean.
The structure is as follows:
There are 2 entity beans: Subject and User mapped onto respective tables and there is a bi-directional many-to-many relationship between them mapped as follows:

public class User implements Serializable{

private Integer userID;
Collection subjectsThisUserSubscribedTo;
public User(String accountName, String email) {
             this.subjectsThisUserSubscribedTo = new rrayList();

@Column(name="UID", nullable = false)
public Integer getUserID() { return userID; }
public void setUserID(Integer uid) { userID = uid; }

public Collection getSubjectsThisUserSubscribedTo() {
		return subjectsThisUserSubscribedTo;
public void setSubjectsThisUserSubscribedTo(
			Collection subjectsThisUserSubscribedTo) {
	this.subjectsThisUserSubscribedTo = subjectsThisUserSubscribedTo;


@Table(name = "subject")
public class Subject implements Serializable{

private Integer subjectID;
Collection usersThatSubscribedToMe;
public Subject(String subjectName, User owner) {
	this.subjectName = subjectName;
	this.owner = owner;
	Collection usersThatSubscribedToMe = new ArrayList();

@Column(name = "SID")
public Integer getSubjectID() { return subjectID;  }
public void setSubjectID(Integer sid) { subjectID = sid; }

public Collection getUsersThatSubscribedToMe() { 
return usersThatSubscribedToMe; 
public void setUsersThatSubscribedToMe(Collection usersThatSubscribedToMe) {
this.usersThatSubscribedToMe = usersThatSubscribedToMe;

Then there is a message bean

public class SubjectBean implements SubjectManager {

	private EntityManager entityManager;


public void addPublicSubject(Subject s, User u) {


When this method is called by the client the Exception noted above is thrown.
Does anyone have a suggestion what is the cause of the problem and how to fix it?

I am running this example on JBoss 5. The server.log is a bit too verbose to paste it here. However, looking through the log I got an impression that JBoss converts statements into prepared queries, but they all seem to range over a single table, being either a subject or a user and there is no mention of the table shared_subject that binds this relationship at the level of my sql schema. I am not sure that I understand what is actually going on there. I also suspect that the problem may not be with evaluating the rationships but with something else. Maybe the cause is that the entity bean is detached from the EntityManager? If so, how to attach it back. What are the rules? It is not obvious to me that I am doing something wrong in my classes. Maybe I missed the point somewhere?

Many thanks to those of you who doesn't mind taking a moment to help a programming novice in trouble.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4145651#4145651

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4145651

More information about the jboss-user mailing list