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 ]