Hibernate / EclipseLink Interview questions
What is a fetch group in EclipseLink?
A fetch group defines a specific subset of an entity's attributes to load in a given query, instead of always fetching every mapped field. This lets an application load only the columns it actually needs for a given use case, deferring the rest until (and unless) they're accessed later.
EntityManager em = ...; FetchGroup fg = new FetchGroup(); fg.addAttribute("name"); fg.addAttribute("department.name"); TypedQuery<Employee> query = em.createQuery( "SELECT e FROM Employee e", Employee.class); query.setHint("eclipselink.fetch-group", fg);
Because weaving instruments the entity class to support partial loading transparently, accessing an attribute outside the fetch group triggers a separate fetch on demand rather than failing, so fetch groups are an optimization rather than a strict contract the calling code has to honor.
More Related questions...