Skip to content

Commit

Permalink
HHH-19034 Fix use of findCompatibleFetchJoin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DavideD authored and mbladel committed Jan 31, 2025
1 parent ff8ebd5 commit 6a3ef4b
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,11 @@ private <A> SqmAttributeJoin<T, A> buildJoin(
SqmPathSource<A> joinedPathSource,
SqmJoinType joinType,
boolean fetched) {
final SqmAttributeJoin<T, A> compatibleFetchJoin = findCompatibleFetchJoin( this, joinedPathSource, joinType );
if ( compatibleFetchJoin != null ) {
return compatibleFetchJoin;
if ( fetched ) {
final SqmAttributeJoin<T, A> compatibleFetchJoin = findCompatibleFetchJoin( this, joinedPathSource, joinType );
if ( compatibleFetchJoin != null ) {
return compatibleFetchJoin;
}
}

final SqmAttributeJoin<T, A> sqmJoin;
Expand Down

0 comments on commit 6a3ef4b

Please sign in to comment.