Lazy loading, Hibernate, and web apps
One problem that I have been having developing TeamDocs is in the area of dealing with Lazy Loading and Hibernate. Specifically, when I load some objects using a DAO and then send those objects to the View to be loaded I kept getting an exception: LazyInitializationException: Session has been closed. This has been driving me nuts. I finally “found the solution”:http://www.hibernate.org/43.html in a post on the “Hibernate webiste”:http://www.hibernate.org.
bq. A first solution would be to open another unit of work for rendering the view. This can easily be done but is usually not the right approach. Rendering the view for a completed action is supposed to be inside the first unit of work, not a separate one. The solution, in two-tiered systems, with the action execution, data access through the Session, and the rendering of the view all in the same virtual machine, is to keep the Session open until the view has been rendered.
Unfortunately, I don’t fully understand the solution yet, but I do know it works.
The above article on the Hibernate website goes along with this one on “Sessions and Transactions”:http://www.hibernate.org/42.html which describes concepts like “Units of Work”, “Transactions”, “Transaction Demarcation with JDBC”, and other topics. You should probably read this article before the one listed above.