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.
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.
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.
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 ]
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.
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).
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?