Can someone point me to a particularly good resource on JPA/Hibernate lazy/eager fetching?
Looking开发者_高级运维 for a really good article that includes strategies/bugs/workarounds. I would prefer a pure JPA solution but I know Hibernate offers a lot of extensions.
I'm not sure of what you're looking for exactly. But to put is simply, LAZY
simply means that a child association won't be loaded while loading the parent, it will be loaded when explicitly asked by the application. EAGER means that a child association will be loaded while loading the parent.
In general, LAZY
is better performance wise (when you just don't need or want to load a whole objects graph). But depending on the situation, it might just be suboptimal or lead to the famous "N+1 SELECT" problem (while iterating over a list of N entities, accessing a LAZY
association will fire another SELECT, resulting in 1+N SELECT at the end). Depending on the situation, an EAGER
association - or a FETCH JOIN
to prefetch the association - is preferable.
So I don't know what part is unclear but here are some resources that might help:
- Hibernate wiki
- A Short Primer On Fetching Strategies (from the pre JPA era but the concepts still apply)
- Some explanations on lazy loading (one-to-one)
- Articles
- Hibernate: Understanding Lazy Fetching
- Hibernate: Tuning Lazy Fetching
See also:
- Hibernate Core documentation
- 14.3. Associations and joins
- 19.1. Fetching strategies
- JPA 1.0 specification
- Section 4.4.5.3 "Fetch Joins"
Book "Java Persistence with Hibernate" by Bauer and King, Chapter 13 "Optimizing fetching and caching". You should be able to find a copy of the book online.
精彩评论