Search code examples
javahibernatejpapersistence

Does EAGER FetchType populates to its children


For example I have a following class structure

class Parent {

fetch = FetchType.EAGER
List<Child> children;
}

class Child {
fetch = FetchType.LAZY
List<SuperChild> superChildren;

}

The question is when I fetching Parent object, e.g:

ParentRepository::findById();

Will superChildren be fetched because of their parents EAGER or their LAZY overrides it?


Solution

  • Nothing about the documentation of FetchType implies any behavior with regards to an associated entity's own FetchType. Note even the description of LAZY (this is from the documentation of the JPA specification):

    "The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch data for which the LAZY strategy hint has been specified."

    So as a side note, in general LAZY may not be guaranteed depending on the implementation provider.

    Hibernate (the most popular JPA implementation) will lazily fetch superChildren, so the FetchType of Parent#children will not matter. I haven't tried with other implementations but I highly doubt it will differ.