File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes LazyInitializationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Reply locked New topic


Anna Madhusudhanan

Joined: Jan 03, 2006
Posts: 8
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

@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.
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Hibernate is trying to save you some overhead if you load the Employee object but don't need the Department object. Basically hibernate returns a proxy object that doesn't load the Department from the database until you actually call it. However that causes a problem when you open the session in your DAO and then the session closes when your DAO completes. Then you return the Employee object to your web layer and it called getDepartment which the proxy tries to load from the database and discovers there in no session and throws an Exception.

Understanding Lazy Fetching
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Thanks for cross posting

Next time Carefully Choose One Forum
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

Please do not cross-post the same question in multiple forums. It wastes people's time when multiple redundant conversations take place. Please read this for more information.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: LazyInitializationException
It's not a secret anymore!