persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Clarify the semantics of Fetch Joins

Open schauder opened this issue 3 years ago • 0 comments

I created a sample to experiment with the behaviour of Fetch Joins vs normal Joins in JPQL and Criteria API. The experiment uses Hibernate as the most popular JPA implementation.

I found the following facts regarding Fetch Join which IMHO sometimes seem to contradict the specification if not in words then in spirit.

A clarification on the Specification both on this issue and in the actual specification would be highly appreciated.

  • Turning a Join into a Fetch Join might multiply the number of parent entities by their number of children. This seems to actually be covered by the specification:

    A fetch join has the same join semantics as the corresponding inner or outer join, except that the related objects specified on the right-hand side of the join operation are not returned in the query result or otherwise referenced in the query. Hence, for example, if department 1 has five employees, the above query returns five references to the department 1 entity. Section 4.4.5.3 of the JPA Specification

  • getSingleResult succeeds if a single entity is returned multiple times due to a Fetch Join. This seems to be in violation of the Specification which in the JavaDoc of getSingleResult says:

    @throws NonUniqueResultException if more than one result

  • A fetch join might implicitely be referenced by a path expression in the WHERE-clause in JPQL, but not in the Criteria API. If done so, the number of children returned is limited by the condition. The behaviour is undefined by the Specification but the possibility to do so seems in contradiction to the specification which explicitely mentions

    It is not permitted to specify an identification variable for the objects referenced by the right side of the FETCH JOIN clause, and hence references to the implicitly fetched entities or elements cannot appear elsewhere in the query.

  • A fetch join actually can be reused in the Criteria API by Hibernate once one realises that a Fetch implementation of Hibernate may be cast to a Join and then used as such.

All the tests are in the single test class: https://github.com/schauder/jpa-hibernate-fetch-join-examples/blob/main/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java

schauder avatar Jul 19 '21 13:07 schauder