Community
Participate
Working Groups
Equivalent JPA statement: Nested @FetchJoin is ignored. org.eclipse.persistence.testing.models.jpa.advance.Employee defines @ManyToOne(fetch=EAGER) @JoinColumn(name="DEPT_ID") @JoinFetch(JoinFetchType.OUTER) public Department getDepartment() { return department; } "SELECT e FROM Employee WHERE e.id = 1" does the outer join with Department: SELECT t1.EMP_ID, t2.EMP_ID, t1.PAY_SCALE, t1.L_NAME, t1.STATUS, t1.VERSION, t1.ROOM_NUM, t1.GENDER, t2.SALARY, t1.F_NAME, t1.START_OVERTIME, t1.END_OVERTIME, t1.START_TIME, t1.END_TIME, t1.ADDR_ID, t1.MANAGER_EMP_ID, t1.DEPT_ID, t1.START_DATE, t1.END_DATE, t1.FORMER_COMPANY, t1.FORMER_START_DATE, t1.FORMER_END_DATE, t0.ID, t0.NAME FROM CMP3_EMPLOYEE t1 LEFT OUTER JOIN CMP3_DEPT t0 ON (t0.ID = t1.DEPT_ID), CMP3_SALARY t2 WHERE ((t1.EMP_ID = 1) AND (t2.EMP_ID = t1.EMP_ID)) Now add a left fetch join to the query: query.setHint(QueryHints.LEFT_FETCH, "e.manager"); The resulting sql doesn't have an OUTER JOIN from e.manager to Department: SELECT t2.EMP_ID, t3.EMP_ID, t2.PAY_SCALE, t2.L_NAME, t2.STATUS, t2.VERSION, t2.ROOM_NUM, t2.GENDER, t3.SALARY, t2.F_NAME, t2.START_OVERTIME, t2.END_OVERTIME, t2.START_TIME, t2.END_TIME, t2.ADDR_ID, t2.MANAGER_EMP_ID, t2.DEPT_ID, t2.START_DATE, t2.END_DATE, t2.FORMER_COMPANY, t2.FORMER_START_DATE, t2.FORMER_END_DATE, t0.EMP_ID, t1.EMP_ID, t0.PAY_SCALE, t0.L_NAME, t0.STATUS, t0.VERSION, t0.ROOM_NUM, t0.GENDER, t1.SALARY, t0.F_NAME, t0.START_OVERTIME, t0.END_OVERTIME, t0.START_TIME, t0.END_TIME, t0.ADDR_ID, t0.MANAGER_EMP_ID, t0.DEPT_ID, t0.START_DATE, t0.END_DATE, t0.FORMER_COMPANY, t0.FORMER_START_DATE, t0.FORMER_END_DATE, t4.ID, t4.NAME FROM CMP3_EMPLOYEE t2 LEFT OUTER JOIN (CMP3_EMPLOYEE t0 JOIN CMP3_SALARY t1 ON (t1.EMP_ID = t0.EMP_ID)) ON (t0.EMP_ID = t2.MANAGER_EMP_ID) LEFT OUTER JOIN CMP3_DEPT t4 ON (t4.ID = t2.DEPT_ID), CMP3_SALARY t3 WHERE ((t2.EMP_ID = ?) AND (t3.EMP_ID = t2.EMP_ID))
James says that not supporting nesting is intentional: otherwise could get into an infinite loop (for instance, if Employee's manager attribute is annotated with @JoinFetch(JoinFetchType.OUTER)). I thought that it would always make sense to annotate with EAGER ToOne relationship with @JoinFetch(JoinFetchType.OUTER)), owever James pointed that in OneToMany case that would mean *always* reading the referenced object (Department) from the data base instead of taking advantage of caching. Still I believe sometimes well thought nested joins could be beneficial (probably only for EAGER OneToOne), especially if meant to be used together with fetch groups. Not sure whether this should be fixed or closed as "Not a bug".
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink