Fix throw IllegalArgumentException for query with no roots specified
This commit is contained in:
parent
5a549ea5b4
commit
bd2446a5d5
|
@ -12,6 +12,7 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||||
import org.hibernate.metamodel.mapping.CollectionPart;
|
import org.hibernate.metamodel.mapping.CollectionPart;
|
||||||
import org.hibernate.metamodel.model.domain.EmbeddableDomainType;
|
import org.hibernate.metamodel.model.domain.EmbeddableDomainType;
|
||||||
import org.hibernate.query.sqm.FetchClauseType;
|
import org.hibernate.query.sqm.FetchClauseType;
|
||||||
|
@ -430,8 +431,14 @@ public class SqmQuerySpec<T> extends SqmQueryPart<T>
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final Set<SqmFrom<?, ?>> selectedFromSet;
|
final Set<SqmFrom<?, ?>> selectedFromSet;
|
||||||
|
final List<SqmRoot<?>> roots = getFromClause().getRoots();
|
||||||
if ( selectClause == null || selectClause.getSelections().isEmpty() ) {
|
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 {
|
else {
|
||||||
selectedFromSet = new HashSet<>( selectClause.getSelections().size() );
|
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 );
|
validateFetchOwners( selectedFromSet, root );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,12 +11,14 @@ import java.util.List;
|
||||||
|
|
||||||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
||||||
import org.hibernate.testing.orm.junit.Jpa;
|
import org.hibernate.testing.orm.junit.Jpa;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import jakarta.persistence.ManyToOne;
|
import jakarta.persistence.ManyToOne;
|
||||||
import jakarta.persistence.Tuple;
|
import jakarta.persistence.Tuple;
|
||||||
|
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||||
import jakarta.persistence.criteria.CriteriaQuery;
|
import jakarta.persistence.criteria.CriteriaQuery;
|
||||||
import jakarta.persistence.criteria.Fetch;
|
import jakarta.persistence.criteria.Fetch;
|
||||||
import jakarta.persistence.criteria.From;
|
import jakarta.persistence.criteria.From;
|
||||||
|
@ -25,8 +27,6 @@ import jakarta.persistence.criteria.JoinType;
|
||||||
import jakarta.persistence.criteria.Root;
|
import jakarta.persistence.criteria.Root;
|
||||||
import jakarta.persistence.criteria.Selection;
|
import jakarta.persistence.criteria.Selection;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
|
||||||
|
|
||||||
@Jpa(
|
@Jpa(
|
||||||
annotatedClasses = {
|
annotatedClasses = {
|
||||||
IllegalArgumentExceptionTest.Person.class,
|
IllegalArgumentExceptionTest.Person.class,
|
||||||
|
@ -41,17 +41,16 @@ public class IllegalArgumentExceptionTest {
|
||||||
final CriteriaQuery<Tuple> query = scope.getEntityManagerFactory().getCriteriaBuilder().createTupleQuery();
|
final CriteriaQuery<Tuple> query = scope.getEntityManagerFactory().getCriteriaBuilder().createTupleQuery();
|
||||||
final Root<Person> person = query.from( Person.class );
|
final Root<Person> person = query.from( Person.class );
|
||||||
|
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> {
|
||||||
List list = new ArrayList();
|
List list = new ArrayList();
|
||||||
list.add( person.get( "id" ).alias( "a" ) );
|
list.add( person.get( "id" ).alias( "a" ) );
|
||||||
list.add( person.get( "name" ).alias( "a" ) );
|
list.add( person.get( "name" ).alias( "a" ) );
|
||||||
|
|
||||||
query.multiselect( list );
|
query.multiselect( list );
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException iae) {
|
|
||||||
//expected by TCK
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -65,13 +64,11 @@ public class IllegalArgumentExceptionTest {
|
||||||
person.get( "name" ).alias( "a" )
|
person.get( "name" ).alias( "a" )
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
query.multiselect( selection );
|
IllegalArgumentException.class,
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
() ->
|
||||||
}
|
query.multiselect( selection )
|
||||||
catch (IllegalArgumentException iae) {
|
);
|
||||||
//expected by TCK
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -80,16 +77,14 @@ public class IllegalArgumentExceptionTest {
|
||||||
final CriteriaQuery<Tuple> query = scope.getEntityManagerFactory().getCriteriaBuilder().createTupleQuery();
|
final CriteriaQuery<Tuple> query = scope.getEntityManagerFactory().getCriteriaBuilder().createTupleQuery();
|
||||||
final Root<Person> person = query.from( Person.class );
|
final Root<Person> person = query.from( Person.class );
|
||||||
|
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() ->
|
||||||
query.multiselect(
|
query.multiselect(
|
||||||
person.get( "not_existing_attribute_name" ).alias( "a1" ),
|
person.get( "not_existing_attribute_name" ).alias( "a1" ),
|
||||||
person.get( "another_not_existing_attribute_name" ).alias( "a2" )
|
person.get( "another_not_existing_attribute_name" ).alias( "a2" )
|
||||||
|
)
|
||||||
);
|
);
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException iae) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -98,29 +93,27 @@ public class IllegalArgumentExceptionTest {
|
||||||
final CriteriaQuery<String> query = scope.getEntityManagerFactory()
|
final CriteriaQuery<String> query = scope.getEntityManagerFactory()
|
||||||
.getCriteriaBuilder()
|
.getCriteriaBuilder()
|
||||||
.createQuery( String.class );
|
.createQuery( String.class );
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> {
|
||||||
final Root<Person> person = query.from( Person.class );
|
final Root<Person> person = query.from( Person.class );
|
||||||
person.get( "not_existing_attribute_name" );
|
person.get( "not_existing_attribute_name" );
|
||||||
|
}
|
||||||
|
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
);
|
||||||
}
|
|
||||||
catch (IllegalArgumentException iae) {
|
|
||||||
//expected by TCK
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetStringNonExistingAttributeName(EntityManagerFactoryScope scope) {
|
public void testGetStringNonExistingAttributeName(EntityManagerFactoryScope scope) {
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> {
|
||||||
final CriteriaQuery<Person> query = scope.getEntityManagerFactory()
|
final CriteriaQuery<Person> query = scope.getEntityManagerFactory()
|
||||||
.getCriteriaBuilder()
|
.getCriteriaBuilder()
|
||||||
.createQuery( Person.class );
|
.createQuery( Person.class );
|
||||||
query.from( Person.class ).get( "not_existing_attribute_name" );
|
query.from( Person.class ).get( "not_existing_attribute_name" );
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException iae) {
|
|
||||||
//expected by TCK
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -129,13 +122,11 @@ public class IllegalArgumentExceptionTest {
|
||||||
.getCriteriaBuilder()
|
.getCriteriaBuilder()
|
||||||
.createQuery( Person.class );
|
.createQuery( Person.class );
|
||||||
final From<Person, Person> customer = query.from( Person.class );
|
final From<Person, Person> customer = query.from( Person.class );
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
customer.join( "not_existing_attribute_name" );
|
IllegalArgumentException.class,
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
() ->
|
||||||
}
|
customer.join( "not_existing_attribute_name" )
|
||||||
catch (IllegalArgumentException iae) {
|
);
|
||||||
//expected by TCK
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -144,13 +135,11 @@ public class IllegalArgumentExceptionTest {
|
||||||
.getCriteriaBuilder()
|
.getCriteriaBuilder()
|
||||||
.createQuery( Person.class );
|
.createQuery( Person.class );
|
||||||
final From<Person, Person> customer = query.from( Person.class );
|
final From<Person, Person> customer = query.from( Person.class );
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
customer.join( "not_existing_attribute_name", JoinType.INNER );
|
IllegalArgumentException.class,
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
() ->
|
||||||
}
|
customer.join( "not_existing_attribute_name", JoinType.INNER )
|
||||||
catch (IllegalArgumentException iae) {
|
);
|
||||||
//expected by TCK
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -161,13 +150,11 @@ public class IllegalArgumentExceptionTest {
|
||||||
|
|
||||||
final Root<Person> customer = query.from( Person.class );
|
final Root<Person> customer = query.from( Person.class );
|
||||||
final Join<Person, Address> address = customer.join( "address" );
|
final Join<Person, Address> address = customer.join( "address" );
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
address.join( "not_existing_attribute_name" );
|
IllegalArgumentException.class,
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
() ->
|
||||||
}
|
address.join( "not_existing_attribute_name" )
|
||||||
catch (IllegalArgumentException iae) {
|
);
|
||||||
//expected by TCK
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -178,13 +165,11 @@ public class IllegalArgumentExceptionTest {
|
||||||
|
|
||||||
final Root<Person> customer = query.from( Person.class );
|
final Root<Person> customer = query.from( Person.class );
|
||||||
final Join<Person, Address> address = customer.join( "address" );
|
final Join<Person, Address> address = customer.join( "address" );
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
address.join( "not_existing_attribute_name", JoinType.INNER );
|
IllegalArgumentException.class,
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
() ->
|
||||||
}
|
address.join( "not_existing_attribute_name", JoinType.INNER )
|
||||||
catch (IllegalArgumentException iae) {
|
);
|
||||||
//expected by TCK
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -196,13 +181,56 @@ public class IllegalArgumentExceptionTest {
|
||||||
final From<Person, Person> customer = query.from( Person.class );
|
final From<Person, Person> customer = query.from( Person.class );
|
||||||
final Fetch f = customer.fetch( "address" );
|
final Fetch f = customer.fetch( "address" );
|
||||||
|
|
||||||
try {
|
Assertions.assertThrows(
|
||||||
f.fetch( "not_existing_attribute_name" );
|
IllegalArgumentException.class,
|
||||||
fail( "TCK expects an IllegalArgumentException" );
|
() ->
|
||||||
|
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")
|
@Entity(name = "Person")
|
||||||
|
|
Loading…
Reference in New Issue