From ef980cbb5da3fe0eca2ec620ff179500deffdcbf Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Sat, 15 Jan 2022 19:24:03 +0100 Subject: [PATCH] Throw IllegalArgumentException when the an attribute name is not resolved as a subPath --- .../internal/QualifiedJoinPathConsumer.java | 12 +-------- .../hibernate/query/sqm/SqmPathSource.java | 3 +++ .../sqm/tree/domain/AbstractSqmFrom.java | 2 +- .../IllegalArgumentExceptionTest.java | 27 ++++++++++++++++--- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java index f43bb2bcc4..d08cc6e629 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java +++ b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/QualifiedJoinPathConsumer.java @@ -176,17 +176,7 @@ public class QualifiedJoinPathConsumer implements DotIdentifierConsumer { boolean isTerminal, SqmCreationState creationState) { //noinspection unchecked - final SqmPathSource subPathSource = (SqmPathSource) lhs.getReferencedPathSource().findSubPathSource( name ); - if ( subPathSource == null ) { - throw new HqlInterpretationException( - String.format( - Locale.ROOT, - "Could not resolve joined attribute '%s' of '%s'", - name, - lhs.getNavigablePath() - ) - ); - } + final SqmPathSource subPathSource = (SqmPathSource) lhs.getReferencedPathSource().getSubPathSource( name ); if ( !isTerminal ) { for ( SqmJoin sqmJoin : lhs.getSqmJoins() ) { if ( sqmJoin.getAlias() == null && sqmJoin.getReferencedPathSource() == subPathSource ) { diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/SqmPathSource.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/SqmPathSource.java index 8099c9e7b1..d573c49e6f 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/SqmPathSource.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/SqmPathSource.java @@ -6,6 +6,8 @@ */ package org.hibernate.query.sqm; +import java.util.Locale; + import jakarta.persistence.metamodel.Bindable; import org.hibernate.metamodel.model.domain.DomainType; @@ -56,6 +58,7 @@ public interface SqmPathSource extends SqmExpressable, Bindable, SqmExp throw new IllegalArgumentException( new SemanticException( String.format( + Locale.ROOT, "Could not resolve attribute '%s' of '%s'", name, getExpressable().getExpressableJavaTypeDescriptor().getJavaType().getTypeName() diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/AbstractSqmFrom.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/AbstractSqmFrom.java index 731147a9ca..6fe78db5c1 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/AbstractSqmFrom.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/AbstractSqmFrom.java @@ -584,7 +584,7 @@ public abstract class AbstractSqmFrom extends AbstractSqmPath implements @SuppressWarnings("unchecked") public SqmAttributeJoin fetch(String attributeName, JoinType jt) { final SqmPathSource fetchedPathSource = (SqmPathSource) getReferencedPathSource() - .findSubPathSource( attributeName ); + .getSubPathSource( attributeName ); return (SqmAttributeJoin) buildJoin( fetchedPathSource, SqmJoinType.from( jt ), diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/IllegalArgumentExceptionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/IllegalArgumentExceptionTest.java index 371c50d218..a37e248c3c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/IllegalArgumentExceptionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/IllegalArgumentExceptionTest.java @@ -18,6 +18,7 @@ import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.Tuple; import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Fetch; import jakarta.persistence.criteria.From; import jakarta.persistence.criteria.Join; import jakarta.persistence.criteria.JoinType; @@ -158,8 +159,8 @@ public class IllegalArgumentExceptionTest { .getCriteriaBuilder() .createQuery( Person.class ); - Root customer = query.from( Person.class ); - Join address = customer.join( "address" ); + final Root customer = query.from( Person.class ); + final Join address = customer.join( "address" ); try { address.join( "not_existing_attribute_name" ); fail( "TCK expects an IllegalArgumentException" ); @@ -175,8 +176,8 @@ public class IllegalArgumentExceptionTest { .getCriteriaBuilder() .createQuery( Person.class ); - Root customer = query.from( Person.class ); - Join address = customer.join( "address" ); + final Root customer = query.from( Person.class ); + final Join address = customer.join( "address" ); try { address.join( "not_existing_attribute_name", JoinType.INNER ); fail( "TCK expects an IllegalArgumentException" ); @@ -186,6 +187,24 @@ public class IllegalArgumentExceptionTest { } } + @Test + public void fetchFetchStringIllegalArgumentExceptionTest(EntityManagerFactoryScope scope) { + final CriteriaQuery query = scope.getEntityManagerFactory() + .getCriteriaBuilder() + .createQuery( Person.class ); + + final From customer = query.from( Person.class ); + final Fetch f = customer.fetch( "address" ); + + try { + f.fetch( "not_existing_attribute_name" ); + fail( "TCK expects an IllegalArgumentException" ); + } + catch (IllegalArgumentException iae) { + //expected by TCK + } + } + @Entity(name = "Person") public static class Person {