my dog learned polymorphism*
The moose likes Object Relational Mapping and the fly likes LazyInitializationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "LazyInitializationException" Watch "LazyInitializationException" New topic
Author

LazyInitializationException

Anna Madhusudhanan
Greenhorn

Joined: Jan 03, 2006
Posts: 8
Hi
I am getting the "org.hibernate.LazyInitializationException: could not initialize proxy - no Session" error while running my application:
I am using the Restful Webservices in GlassFish application server. I configured the Hibernate properties in the persistence.xml file. (GlassFish App server + Hibernate + Restful Webservices(jersey)).
I am using the Lazy loading.

Assume these is the situation:

I have Employee object and Department object.

The department object is declared as the member variable in the Employee object.

Public class Employee {

public int empno;
public String empName;
public Department department;

public void setDepartment(Department dept){
//......
//......
//do something
}

@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "XYZ")
public Department getDepartment(){
//......
//......
//do something
}



//setter and getting for each methods
}

public class Department {
//......
//......
}


i am getting this Employee object using EntityManger find method .
I got the employee object and if i exeucte the getEmpNo oir getEmpName , i am getting the value.

but if i try to get the Department(while running the getDepartment method),
I am getting this

"org.hibernate.LazyInitializationException: could not initialize proxy - no Session"

Please could you explaing me , where is the problem and which one causing the problem.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

This exception occurs when you don't have all your data in your objects, then leave the confines of a Session object.

So if I lazy load a Collection say Parent has a Collection of Children. If Children is lazy loaded and I call getChildren in the Parent object, and there is no Session open, then yoiu have no way to load that data, so it throws that exception. Either eager load the data when you query, or call the code within a Session.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Anna Madhusudhanan
Greenhorn

Joined: Jan 03, 2006
Posts: 8
Hi, Waht you are telling is correct.
To retrive the Employee object , if i use Session object it is working fine. if i use EntityManager , it is not working.

// ERROR GIVING CODE...
javax.persistence.EntityManager manager = getManager(); //JNDI LOOKUP

EMployee emp = manager.find(Employee.class, new Long(empNo));
//i got the emp object

String name = emp.getName(); // This returns me Employee name.

//if i try this line i am getting error.

int departmentNo = emp.getDepartment().getDepartmentNo();

In the above line i am getting the
"org.hibernate.LazyInitializationException: could not initialize proxy - no Session", session is closed.


//WORKING CODE

javax.persistence.EntityManager manager = getManager(); //JNDI LOOKUP

EntityManagerImpl entityImpl =
(EntityManagerImpl) entityManager.getDelegate();

org.hibernate.Session session = entityManagerImpl.getSession();

Employee emp = session.get(Employee.class, new Long(empNo));

String name = emp.getName();

int deptNo = emp.getDepartment().getDepartmentNo();

session.close();

These lines are working fine.

In the first model, i am getting the error.

I am using Jersey(RESTFUL WEBSERVICES), GlassFish server, Hibernate.

Am i missing any confiuration. Or anything?

As per assumption is, When we call entitymanager.find method, the entitymanager opens a session and create the employee object, close the session and then return the employee object.

So at the time of calling the emp.getDepartment().getDepartmentNo() method,The code tries to get the department object, but the session is closed, so we are getting this error.
If my assumption is correct, my question is how can i get the department object from the Employee object?

Please help me.

Thanks
Anna
[ July 22, 2008: Message edited by: Anna Madhusudhanan ]
mady sady
Greenhorn

Joined: Feb 25, 2009
Posts: 3
You need to set lazy="false" in hibernate mapping file.
for more details check http://java-masters.blogspot.com/ check here
Ran Pleasant
Ranch Hand

Joined: Jan 16, 2003
Posts: 75
Mark Spritzler wrote:This exception occurs when you don't have all your data in your objects, then leave the confines of a Session object.

So if I lazy load a Collection say Parent has a Collection of Children. If Children is lazy loaded and I call getChildren in the Parent object, and there is no Session open, then yoiu have no way to load that data, so it throws that exception. Either eager load the data when you query, or call the code within a Session.


Mark

If the collection is marked as Lazy you can only access it within the confines of the Session. If you successfully make a call to the collection within the confines of the Session and then move outside of the confines of the Session it will still throw the exception. This is because the Session object is actually stored within your entity object and that Session object is accessed every time the getter method is called.

Someone please correct me if the above is wrong.

Ran


Ran
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


This is because the Session object is actually stored within your entity object and that Session object is accessed every time the getter method is called

No. If the collection is initialized there is no need to go back to the database, so you can use the object in its current state safely enough as a detached object (i.e. outside a session).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ran Pleasant
Ranch Hand

Joined: Jan 16, 2003
Posts: 75
Paul

Thanks for clearing that up.

Ran
Vladimir Galcenco
Greenhorn

Joined: Mar 23, 2011
Posts: 4
Hi,

I think the first thing to keep in mind here is that lazy loading should work when Employee entity is attached(managed) to the persistence context.
In order for the entity to be attached to a persistence context you should be running within an active JTA transaction with a Transaction scoped entity manager.
Alternatively, if you use an Extended Persistence context your entities remain managed even if no transaction is present.

1) A transaction would be required for the following code:


2) No transaction required if you use a an extended PersistenceContext, although this code is valid in Stateful beans only, or in J2SE (RESOURCE_LOCAL) persistence context.


By the way is there a reason why you obtain your EntityManager via JNDI lookup?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LazyInitializationException
 
Similar Threads
LazyInitializationException
LazyInitializationException
Old Issue, but not finding a solution: org.hibernate.LazyInitializationException
Spring 2.5 with Hibernate 3.0 error when creating dataSource for Hibernate SessionFactory
org.hibernate.Exception: could not initialize proxy - the owning Se