Throw IllegalArgumentException when the an attribute name is not resolved as a subPath

This commit is contained in:
Andrea Boriero 2022-01-15 19:24:03 +01:00 committed by Andrea Boriero
parent 9fba739bc2
commit ef980cbb5d
4 changed files with 28 additions and 16 deletions

View File

@ -176,17 +176,7 @@ public class QualifiedJoinPathConsumer implements DotIdentifierConsumer {
boolean isTerminal, boolean isTerminal,
SqmCreationState creationState) { SqmCreationState creationState) {
//noinspection unchecked //noinspection unchecked
final SqmPathSource<Object> subPathSource = (SqmPathSource<Object>) lhs.getReferencedPathSource().findSubPathSource( name ); final SqmPathSource<Object> subPathSource = (SqmPathSource<Object>) lhs.getReferencedPathSource().getSubPathSource( name );
if ( subPathSource == null ) {
throw new HqlInterpretationException(
String.format(
Locale.ROOT,
"Could not resolve joined attribute '%s' of '%s'",
name,
lhs.getNavigablePath()
)
);
}
if ( !isTerminal ) { if ( !isTerminal ) {
for ( SqmJoin<?, ?> sqmJoin : lhs.getSqmJoins() ) { for ( SqmJoin<?, ?> sqmJoin : lhs.getSqmJoins() ) {
if ( sqmJoin.getAlias() == null && sqmJoin.getReferencedPathSource() == subPathSource ) { if ( sqmJoin.getAlias() == null && sqmJoin.getReferencedPathSource() == subPathSource ) {

View File

@ -6,6 +6,8 @@
*/ */
package org.hibernate.query.sqm; package org.hibernate.query.sqm;
import java.util.Locale;
import jakarta.persistence.metamodel.Bindable; import jakarta.persistence.metamodel.Bindable;
import org.hibernate.metamodel.model.domain.DomainType; import org.hibernate.metamodel.model.domain.DomainType;
@ -56,6 +58,7 @@ public interface SqmPathSource<J> extends SqmExpressable<J>, Bindable<J>, SqmExp
throw new IllegalArgumentException( throw new IllegalArgumentException(
new SemanticException( new SemanticException(
String.format( String.format(
Locale.ROOT,
"Could not resolve attribute '%s' of '%s'", "Could not resolve attribute '%s' of '%s'",
name, name,
getExpressable().getExpressableJavaTypeDescriptor().getJavaType().getTypeName() getExpressable().getExpressableJavaTypeDescriptor().getJavaType().getTypeName()

View File

@ -584,7 +584,7 @@ public abstract class AbstractSqmFrom<O,T> extends AbstractSqmPath<T> implements
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <X, A> SqmAttributeJoin<X, A> fetch(String attributeName, JoinType jt) { public <X, A> SqmAttributeJoin<X, A> fetch(String attributeName, JoinType jt) {
final SqmPathSource<A> fetchedPathSource = (SqmPathSource<A>) getReferencedPathSource() final SqmPathSource<A> fetchedPathSource = (SqmPathSource<A>) getReferencedPathSource()
.findSubPathSource( attributeName ); .getSubPathSource( attributeName );
return (SqmAttributeJoin<X, A>) buildJoin( return (SqmAttributeJoin<X, A>) buildJoin(
fetchedPathSource, fetchedPathSource,
SqmJoinType.from( jt ), SqmJoinType.from( jt ),

View File

@ -18,6 +18,7 @@ import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.persistence.Tuple; import jakarta.persistence.Tuple;
import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Fetch;
import jakarta.persistence.criteria.From; import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.Join; import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType; import jakarta.persistence.criteria.JoinType;
@ -158,8 +159,8 @@ public class IllegalArgumentExceptionTest {
.getCriteriaBuilder() .getCriteriaBuilder()
.createQuery( Person.class ); .createQuery( Person.class );
Root<Person> customer = query.from( Person.class ); final Root<Person> customer = query.from( Person.class );
Join<Person, Address> address = customer.join( "address" ); final Join<Person, Address> address = customer.join( "address" );
try { try {
address.join( "not_existing_attribute_name" ); address.join( "not_existing_attribute_name" );
fail( "TCK expects an IllegalArgumentException" ); fail( "TCK expects an IllegalArgumentException" );
@ -175,8 +176,8 @@ public class IllegalArgumentExceptionTest {
.getCriteriaBuilder() .getCriteriaBuilder()
.createQuery( Person.class ); .createQuery( Person.class );
Root<Person> customer = query.from( Person.class ); final Root<Person> customer = query.from( Person.class );
Join<Person, Address> address = customer.join( "address" ); final Join<Person, Address> address = customer.join( "address" );
try { try {
address.join( "not_existing_attribute_name", JoinType.INNER ); address.join( "not_existing_attribute_name", JoinType.INNER );
fail( "TCK expects an IllegalArgumentException" ); fail( "TCK expects an IllegalArgumentException" );
@ -186,6 +187,24 @@ public class IllegalArgumentExceptionTest {
} }
} }
@Test
public void fetchFetchStringIllegalArgumentExceptionTest(EntityManagerFactoryScope scope) {
final CriteriaQuery<Person> query = scope.getEntityManagerFactory()
.getCriteriaBuilder()
.createQuery( Person.class );
final From<Person, Person> 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") @Entity(name = "Person")
public static class Person { public static class Person {