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,
SqmCreationState creationState) {
//noinspection unchecked
final SqmPathSource<Object> subPathSource = (SqmPathSource<Object>) 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<Object> subPathSource = (SqmPathSource<Object>) lhs.getReferencedPathSource().getSubPathSource( name );
if ( !isTerminal ) {
for ( SqmJoin<?, ?> sqmJoin : lhs.getSqmJoins() ) {
if ( sqmJoin.getAlias() == null && sqmJoin.getReferencedPathSource() == subPathSource ) {

View File

@ -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<J> extends SqmExpressable<J>, Bindable<J>, SqmExp
throw new IllegalArgumentException(
new SemanticException(
String.format(
Locale.ROOT,
"Could not resolve attribute '%s' of '%s'",
name,
getExpressable().getExpressableJavaTypeDescriptor().getJavaType().getTypeName()

View File

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

View File

@ -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<Person> customer = query.from( Person.class );
Join<Person, Address> address = customer.join( "address" );
final Root<Person> customer = query.from( Person.class );
final Join<Person, Address> 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<Person> customer = query.from( Person.class );
Join<Person, Address> address = customer.join( "address" );
final Root<Person> customer = query.from( Person.class );
final Join<Person, Address> 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<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")
public static class Person {