Skip to content

Commit ebf06cc

Browse files
DavideDmbellade
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 1022e9c commit ebf06cc

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
@@ -793,9 +793,11 @@ private <A> SqmAttributeJoin<T, A> buildJoin(
793793
SqmPathSource<A> joinedPathSource,
794794
SqmJoinType joinType,
795795
boolean fetched) {
796-
final SqmAttributeJoin<T, A> compatibleFetchJoin = findCompatibleFetchJoin( this, joinedPathSource, joinType );
797-
if ( compatibleFetchJoin != null ) {
798-
return compatibleFetchJoin;
796+
if ( fetched ) {
797+
final SqmAttributeJoin<T, A> compatibleFetchJoin = findCompatibleFetchJoin( this, joinedPathSource, joinType );
798+
if ( compatibleFetchJoin != null ) {
799+
return compatibleFetchJoin;
800+
}
799801
}
800802

801803
final SqmAttributeJoin<T, A> sqmJoin;

0 commit comments

Comments
 (0)