If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Sunday, September 26, 2010

Hibernate and Spring - Part 6

Everybody who uses hibernate in a project is bound to run into the famous LazyInitializationException problem. By default, collections are loaded lazy. That's in most cases also what you want. E.g.: If you query Facebook for users with firstname 'John', you don't want to fetch all available data about those users in 1 go. You only want to get their complete names. If you click on the name, you want more detailed information.

You should definitely read Hibernate Fetching Strategies

Below default behaviour (fetch=FetchType.LAZY) which can be omitted.


One way to make sure you never run into lazyinitalization exceptions is to fetch eagerly. But be aware for the penalty on performance and memory consumption. You might be fetching big parts of your database if you use this strategy.


One other way to solve this issue is to add following snippet to the web.xml

No comments:

Post a Comment