• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

LazyInitializationException

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Anna Madhusudhanan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to set lazy="false" in hibernate mapping file.
for more details check http://java-masters.blogspot.com/ check here
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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).
 
Ran Pleasant
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul

Thanks for clearing that up.

Ran
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
This tiny ad is wafer thin:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic