Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 322702

Summary: Too much fetching with fetch-group and eclipselink.weaving.eager=true
Product: z_Archived Reporter: Frank Schwarz <fs5>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: andrei.ilitchev, douglas.clarke, eclipselink.orm-inbox
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
reproducer
none
suggested patch none

Description Frank Schwarz CLA 2010-08-14 01:29:38 EDT
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
Comment 1 Frank Schwarz CLA 2010-08-14 01:32:27 EDT
Created attachment 176595 [details]
reproducer
Comment 2 Doug Clarke CLA 2010-08-16 02:41:33 EDT
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.
Comment 3 Andrei Ilitchev CLA 2010-08-18 14:34:50 EDT
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.
Comment 4 Andrei Ilitchev CLA 2010-08-19 11:44:49 EDT
Checked the patch into trunk, 2.1.2 is pending.
Comment 5 Andrei Ilitchev CLA 2010-08-31 15:30:30 EDT
Checked the patch into 2.1.2.
Comment 6 Eclipse Webmaster CLA 2022-06-09 10:22:25 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink