I am facing problem with Hibernate Lazy initialization. I tried to make lazy=true in mapping file, to improve the performance of the site. without turning it to true, entire object tree is getting loaded into memory.
Following are the steps I followed
1. I made lazy=true for all classes, and the associations which each class possess. -> I got LazyInitializationException: could not initialize proxy - the owning Session was closed I started adding lazy to one class and it's associated classes. But problem was still there.
2. Added OpenSessionInViewFilter as follows to resolve above exception. <filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3. I started getting -> HibernateSystemException: a different object with the same identifier value was already associated with the session: nested exception is org.hibernate.NonUniqueObjectException
4. To resolve NonUniqueObjectException, I tried to catch it and started using merge() supported by hibernate 3.0 as follows.
try{ getHibernateTemplate().saveOrUpdate(o); } catch(HibernateSystemException hse){ getHibernateTemplate().merge(o); }
5. By doing all above steps I could resolve the problem only for few classes and relations. If I try to enable lazy for others, I still get LazyInitializationException: could not initialize proxy - the owning Session was closed
This lazy problem has taken lot of efforts by now. I would appreciate if any one could suggest where I could be doing wrong.
|