Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question you like me to answer, please leave a comment below.
How can I initialize a lazy relationship within a query to avoid LazyInitializationExceptions?
Hibernate throws a LazyInitializationException if you try to use the attribute of a lazily fetched relationship outside of an active Hibernate Session.
You can avoid that by initializing the relationship before you close the session. Hibernate and JPA support different ways to do that. The easiest one is a JOIN FETCH statement within a query, like the one in the following code snippet.
The additional FETCH keyword tells Hibernate to not only join the entity for the query but also to fetch it from the database to initialize the attribute. This prevents LazyInitializationExceptions if you access the relationship attribute outside of an active Hibernate Session.
Get this Hibernate Tip as a printable PDF!Join the free Thoughts on Java Library to get access to lots of member-only content, like a printable PDF for this post, lots of cheat sheets and 2 ebooks about Hibernate.
Join Fetch statements are only 1 option to initialize lazy relationships. Other interesting options are @NamedEntityGraphs and dynamic entity graphs which allow you to define a query independent graph of entities which shall be fetched with a query.
Initializing a required lazy relationship does not only prevent LazyInitializationExceptions, it also can improve the performance by avoiding n+1 select issues. I get into more details about that in this free mini-course and in the Hibernate Performance Tuning Online Training.
Hibernate Tips Book
It gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries.
Get it now as a paperback, ebook or PDF.