I have a spring-hibernate code which does the following two steps
1. Save an Object to database
2. Retrieve the object saved and display the details of that values saved
To do the above two steps in a row , I saved the
getHibernateTemplate().saveOrUpdate(obj);. To my knowledge this calls the hibernate saveorupdate() method internally and saves it to the database.
After saving to the database it closes the Hibernate session.
In step two, to retrieve the object from the database, I executed
return (Employee)getHibernateTemplate().load(Employee.cla ss, id);. To retrieve data from database the earlier session is closed, now it says,
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
To avoid the above situation, I placed both the statements in a single method and declared it as a transaction (
@Transactional(propagation=Propagation.REQUIRED , rollbackFor=Exception.class). Still the same issue. I am pasting my code below, please kindly look into it. Thanks in advance.
EmployeeDao
App.java
Output
Bean retrived from bean factory ...
Data has been saved
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session