I face a problem while using Hibernate.
I do a search on owner names and get a list of owner id and names and when I click on a particular owner it takes me to the view screen.
To view the details of owner, the method(code piece) which I use is -
session = HibernateSessionFactory.getInstance().currentSession();
tx = session.beginTransaction();
TFmsOwnerInformation tOwner = new TFmsOwnerInformation();
tOwner = (TFmsOwnerInformation) session.load(
TFmsOwnerInformation.class, new Long(ownerId));
list.add(tOwner);
In the finally I do
finally {
if(tx != null) tx.commit();
if (session != null) session.close();
}
But If I do session.close() in finally I get an error saying:
15:12:44,199 ERROR [LazyInitializationException] could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
To avoid this error I used session.flush(). But this created another problem. I have a Update button on the view screen. After I click on Update(the update screen loads the data by calling the same method) and then save the data, I come back to the view screen. Hence the same method is used for view and update screen to load. But it used to display stale data in the view screen/update screen a lot of times.
To avoid this a method was added by us - HibernateSessionFactory.closeHibernateFactory(); before session = HibernateSessionFactory.getInstance().currentSession();
closeHibernateFactory() ::
public static synchronized void closeHibernateFactory() {
s_instance = null;
Session s = (Session) s_session.get();
s = null;
s_session.set(s);
}
But this method again instantiates all the mapping files. Can anybody please give me a solution to this?
|