JPA Lazy Collection Loading

If all you are looking for is how to load a lazy collection with JPA, you’ll be swamped with endless posts and articles going on about the pros and cons of using lazy or eager loading. Its all noise if your question is simply how do I load my lazy collection using the JPA API?

The quick and dirty answer is just do this: singleFoo.getBars().size();

There it is. End of story. It is worth noting that I’m an advocate of using a Service Facade and DTOs, in other words clean APIs and I use Spring’s transactional annotations on the service layer. Also, you will come across posts mentioning Hibernate and initialization methods, this doesn’t help since we are only interested in pure JPA here.

Sure there is alot more to it and there are pitfalls to consider, that is old news, any layer of abstraction comes at it price and in some way dumbs you down (mmm, getting philisophical, its so easy to do).

This is one of the posts I was referring to, and it was a distraction: Avoid Lazy JPA Collections. A better read would be this one: JPA Implementation Patterns: Lazy Loading