Fix throw IllegalArgumentException for query with no roots specified

This commit is contained in:
Andrea Boriero 2022-02-01 11:47:14 +01:00 committed by Andrea Boriero
parent 5a549ea5b4
commit bd2446a5d5
2 changed files with 119 additions and 84 deletions

View File

@ -12,6 +12,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.model.domain.EmbeddableDomainType;
import org.hibernate.query.sqm.FetchClauseType;
@ -430,8 +431,14 @@ public class SqmQuerySpec<T> extends SqmQueryPart<T>
return;
}
final Set<SqmFrom<?, ?>> selectedFromSet;
final List<SqmRoot<?>> roots = getFromClause().getRoots();
if ( selectClause == null || selectClause.getSelections().isEmpty() ) {
selectedFromSet = Collections.singleton( getFromClause().getRoots().get( 0 ) );
if ( CollectionHelper.isEmpty( roots ) ) {
throw new SemanticException( "No query roots were specified" );
}
else {
selectedFromSet = Collections.singleton( roots.get( 0 ) );
}
}
else {
selectedFromSet = new HashSet<>( selectClause.getSelections().size() );
@ -440,7 +447,7 @@ public class SqmQuerySpec<T> extends SqmQueryPart<T>
}
}
for ( SqmRoot<?> root : getFromClause().getRoots() ) {
for ( SqmRoot<?> root : roots ) {
validateFetchOwners( selectedFromSet, root );
}
}

View File

@ -11,12 +11,14 @@ import java.util.List;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Tuple;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Fetch;
import jakarta.persistence.criteria.From;
@ -25,8 +27,6 @@ import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.Selection;
import static org.junit.jupiter.api.Assertions.fail;
@Jpa(
annotatedClasses = {
IllegalArgumentExceptionTest.Person.class,
@ -41,17 +41,16 @@ public class IllegalArgumentExceptionTest {
final CriteriaQuery<Tuple> query = scope.getEntityManagerFactory().getCriteriaBuilder().createTupleQuery();
final Root<Person> person = query.from( Person.class );
try {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
List list = new ArrayList();
list.add( person.get( "id" ).alias( "a" ) );
list.add( person.get( "name" ).alias( "a" ) );
query.multiselect( list );
fail( "TCK expects an IllegalArgumentException" );
}
catch (IllegalArgumentException iae) {
//expected by TCK
}
);
}
@Test
@ -65,13 +64,11 @@ public class IllegalArgumentExceptionTest {
person.get( "name" ).alias( "a" )
};
try {
query.multiselect( selection );
fail( "TCK expects an IllegalArgumentException" );
}
catch (IllegalArgumentException iae) {
//expected by TCK
}
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
query.multiselect( selection )
);
}
@Test
@ -80,16 +77,14 @@ public class IllegalArgumentExceptionTest {
final CriteriaQuery<Tuple> query = scope.getEntityManagerFactory().getCriteriaBuilder().createTupleQuery();
final Root<Person> person = query.from( Person.class );
try {
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
query.multiselect(
person.get( "not_existing_attribute_name" ).alias( "a1" ),
person.get( "another_not_existing_attribute_name" ).alias( "a2" )
)
);
fail( "TCK expects an IllegalArgumentException" );
}
catch (IllegalArgumentException iae) {
// expected
}
}
@Test
@ -98,29 +93,27 @@ public class IllegalArgumentExceptionTest {
final CriteriaQuery<String> query = scope.getEntityManagerFactory()
.getCriteriaBuilder()
.createQuery( String.class );
try {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
final Root<Person> person = query.from( Person.class );
person.get( "not_existing_attribute_name" );
}
fail( "TCK expects an IllegalArgumentException" );
}
catch (IllegalArgumentException iae) {
//expected by TCK
}
);
}
@Test
public void testGetStringNonExistingAttributeName(EntityManagerFactoryScope scope) {
try {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
final CriteriaQuery<Person> query = scope.getEntityManagerFactory()
.getCriteriaBuilder()
.createQuery( Person.class );
query.from( Person.class ).get( "not_existing_attribute_name" );
fail( "TCK expects an IllegalArgumentException" );
}
catch (IllegalArgumentException iae) {
//expected by TCK
}
);
}
@Test
@ -129,13 +122,11 @@ public class IllegalArgumentExceptionTest {
.getCriteriaBuilder()
.createQuery( Person.class );
final From<Person, Person> customer = query.from( Person.class );
try {
customer.join( "not_existing_attribute_name" );
fail( "TCK expects an IllegalArgumentException" );
}
catch (IllegalArgumentException iae) {
//expected by TCK
}
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
customer.join( "not_existing_attribute_name" )
);
}
@Test
@ -144,13 +135,11 @@ public class IllegalArgumentExceptionTest {
.getCriteriaBuilder()
.createQuery( Person.class );
final From<Person, Person> customer = query.from( Person.class );
try {
customer.join( "not_existing_attribute_name", JoinType.INNER );
fail( "TCK expects an IllegalArgumentException" );
}
catch (IllegalArgumentException iae) {
//expected by TCK
}
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
customer.join( "not_existing_attribute_name", JoinType.INNER )
);
}
@Test
@ -161,13 +150,11 @@ public class IllegalArgumentExceptionTest {
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" );
}
catch (IllegalArgumentException iae) {
//expected by TCK
}
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
address.join( "not_existing_attribute_name" )
);
}
@Test
@ -178,13 +165,11 @@ public class IllegalArgumentExceptionTest {
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" );
}
catch (IllegalArgumentException iae) {
//expected by TCK
}
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
address.join( "not_existing_attribute_name", JoinType.INNER )
);
}
@Test
@ -196,13 +181,56 @@ public class IllegalArgumentExceptionTest {
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" );
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
f.fetch( "not_existing_attribute_name" )
);
}
catch (IllegalArgumentException iae) {
//expected by TCK
@Test
public void testHqlQueryWithWrongSemantic(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
entityManager.createQuery( "Seletc p" ).getResultList()
);
}
);
}
@Test
public void testCriteriaNullReturnType(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
CriteriaBuilder criteriaBuilder = scope.getEntityManagerFactory().getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery( null );
entityManager.createQuery( criteriaQuery ).getResultList();
}
);
}
);
}
@Test
public void testQueryWrongReturnType(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
entityManager.createQuery( "select p from Peron p", Integer.class ).getResultList();
}
);
}
);
}
@Entity(name = "Person")