Strategies to avoid Hibernate LazyInitializationExceptions

database, hibernate, java

Solution

When working on our web applications, we usually decide beforehand which objects/fields will be needed in the view pages and make sure that all the objecs are properly initialized from the model before dispatching to the view.

This can be accomplished in (at least) three ways:

- fetching properties using eager strategy (i.e. with `FetchMode.JOIN`, if you're using the Criteria API)

- explicitly initializing properties (i.e. with `Hibernate.initialize(property)`)

- implicitly initializing properties by calling the appropriate property accessor

About the downsides of OpenSessionInView, have you checked out this page?

Problem

Are there any other ways to avoid LazyInitializationExceptions in a Hibernate web application besides using the OpenSessionInView pattern? Are there any downsides to using OpenSessionInView?

Original source

Related problems