Community
Participate
Working Groups
Build Identifier: 2.1.0 Lets say there are two classes Employee and Address with an unidirectional one-to-one-relationship from employee to address. The address field will be eagerly loaded as default. If I then define a FetchGroup as: FetchGroup empGr = new FetchGroup(); empGr.addAttribute("name"); to only fetch the "id" and "name" attributes of Employee, I observe to following behavior with eclipselink.weaving.eager=true: FetchGroup empGr = new FetchGroup(); empGr.addAttribute("name"); TypedQuery<Employee> query = em.createQuery("select e from Employee e where e.id = 6", Employee.class); query.setHint(QueryHints.FETCH_GROUP, empGr); List<Employee> results = query.getResultList(); With the operation 'getResultList()' there are 3 SQL statements logged: SELECT ID, NAME FROM EMPLOYEE WHERE (ID = ?) SELECT ID, STARTDATE, NAME, SALARY, ADDRESS_ID FROM EMPLOYEE WHERE (ID = ?) SELECT ID, ZIP, STREET, STATE, CITY FROM ADDRESS WHERE (ID = ?) I actually was expecting only the first one. If I switch off "eclipselink.weaving.eager", then everything works as intended. Reproducible: Always Steps to Reproduce: see attached reproducer
Created attachment 176595 [details] reproducer
I assume the issue is with the processing of the eager relationships using indirection without taking into consideration the applied fetch-group. As a work-around I would suggest using a default FetchGroup with load enabled. In a DescriptorCustomizer you can setup a default FetchGroup as: public class EmployeeCustomizer implements DescriptorCustomizer { public void customize(ClassDescriptor descriptor) throws Exception { FetchGroup defaultFG = new FetchGroup(); defaultFG.setShouldLoad(true); defaultFG.addAttribute("firstName"); defaultFG.addAttribute("lastName"); defaultFG.addAttribute("salary"); defaultFG.addAttribute("gender"); defaultFG.addAttribute("address"); descriptor.getFetchGroupManager().setDefaultFetchGroup(defaultFG); } } This means that on any query without a FetchGroup configured the specified attributes including 'address' will be loaded. I configure the address attribute to be lazy but the default FetchGroup with setShouldLoad set to true will cause it to be loaded. Note: You must specify all attributes you want loaded in the list as none will be configured based on their mapping's fetch type.
Created attachment 176928 [details] suggested patch Adding <property name="eclipselink.weaving.eager" value="true"/> to eclipselink-annotation-model's persistence.xml and running jpa tests produced dozens of errors in fetch group tests. All fetch group tests pass with the fixed applied.
Checked the patch into trunk, 2.1.2 is pending.
Checked the patch into 2.1.2.
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink