Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 322702 - Too much fetching with fetch-group and eclipselink.weaving.eager=true
Summary: Too much fetching with fetch-group and eclipselink.weaving.eager=true
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-14 01:29 EDT by Frank Schwarz CLA
Modified: 2022-06-09 10:22 EDT (History)
3 users (show)

See Also:


Attachments
reproducer (11.39 KB, application/x-zip-compressed)
2010-08-14 01:32 EDT, Frank Schwarz CLA
no flags Details
suggested patch (1.34 KB, patch)
2010-08-18 14:34 EDT, Andrei Ilitchev CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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