Skip to content

Commit 6a3ef4b

Browse files
DavideDmbladel
authored andcommitted
HHH-19034 Fix use of findCompatibleFetchJoin
The problem was that you had a different SQL query depending on the order the fetch and join operations were created when defining a criteria query. For example: ``` // Example 1: Root<Book> from = criteriaQuery.from( Book.class ); Fetch<Object, Object> fetch = from.fetch( "authors" ); Join<Object, Object> join = from.join( "authors" ); ``` it was different than ``` // Example 2: Root<Book> from = criteriaQuery.from( Book.class ); Join<Object, Object> join = from.join( "authors" ); Fetch<Object, Object> fetch = from.fetch( "authors" ); ``` In the first example, `fetch` and `join` were exactly the same object, causing issues if the association `authors` appeared in the `where` clause. For example: ``` criteriaQuery.where( cb.equal( join.get( "id" ), 2L ) ); ``` Note that now we always rung an extra join even when not necessary. But this is consistent with what happen with HQL, and we can figure out how to add this improvement in a different issue.
1 parent ff8ebd5 commit 6a3ef4b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/AbstractSqmFrom.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,11 @@ private <A> SqmAttributeJoin<T, A> buildJoin(
720720
SqmPathSource<A> joinedPathSource,
721721
SqmJoinType joinType,
722722
boolean fetched) {
723-
final SqmAttributeJoin<T, A> compatibleFetchJoin = findCompatibleFetchJoin( this, joinedPathSource, joinType );
724-
if ( compatibleFetchJoin != null ) {
725-
return compatibleFetchJoin;
723+
if ( fetched ) {
724+
final SqmAttributeJoin<T, A> compatibleFetchJoin = findCompatibleFetchJoin( this, joinedPathSource, joinType );
725+
if ( compatibleFetchJoin != null ) {
726+
return compatibleFetchJoin;
727+
}
726728
}
727729

728730
final SqmAttributeJoin<T, A> sqmJoin;

0 commit comments

Comments
 (0)