Mauro Flores Messages: 84 Registered: September 2009 Location: Brasil
If I have a chain of eager relationships ( A → B → C → D), if I find an instance of the entity A, I get the whole chained objects (A, Bs, Cs and Ds)
Is there a way to limit the number of levels to be fetched eagerly? For instance, I just want the B relationships (first level) should be fetched eagerly.
Unfortunately there isn't a way in JPA other than using Lazy relations or fetch groups. When specifying eager relationships, you are saying that when you fetch Bs you always want to Fetch Cs, and when fetching Cs you always want Ds. It might be better to mark all relationships as lazy, and then fetch join the relationships required in the queries used to bring in the first level entities.
Fetch groups can also be used much like lazy relationships to only return what you want, and can be used at the query level to return levels different than the default settings - compariable to using join fetch.