Skip to content

Commit bd2446a

Browse files
committed
Fix throw IllegalArgumentException for query with no roots specified
1 parent 5a549ea commit bd2446a

File tree

2 files changed

+120
-85
lines changed

2 files changed

+120
-85
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmQuerySpec.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313
import java.util.Set;
1414

15+
import org.hibernate.internal.util.collections.CollectionHelper;
1516
import org.hibernate.metamodel.mapping.CollectionPart;
1617
import org.hibernate.metamodel.model.domain.EmbeddableDomainType;
1718
import org.hibernate.query.sqm.FetchClauseType;
@@ -430,8 +431,14 @@ public void validateFetchOwners() {
430431
return;
431432
}
432433
final Set<SqmFrom<?, ?>> selectedFromSet;
434+
final List<SqmRoot<?>> roots = getFromClause().getRoots();
433435
if ( selectClause == null || selectClause.getSelections().isEmpty() ) {
434-
selectedFromSet = Collections.singleton( getFromClause().getRoots().get( 0 ) );
436+
if ( CollectionHelper.isEmpty( roots ) ) {
437+
throw new SemanticException( "No query roots were specified" );
438+
}
439+
else {
440+
selectedFromSet = Collections.singleton( roots.get( 0 ) );
441+
}
435442
}
436443
else {
437444
selectedFromSet = new HashSet<>( selectClause.getSelections().size() );
@@ -440,7 +447,7 @@ public void validateFetchOwners() {
440447
}
441448
}
442449

443-
for ( SqmRoot<?> root : getFromClause().getRoots() ) {
450+
for ( SqmRoot<?> root : roots ) {
444451
validateFetchOwners( selectedFromSet, root );
445452
}
446453
}

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/IllegalArgumentExceptionTest.java

Lines changed: 111 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
1313
import org.hibernate.testing.orm.junit.Jpa;
14+
import org.junit.jupiter.api.Assertions;
1415
import org.junit.jupiter.api.Test;
1516

1617
import jakarta.persistence.Entity;
1718
import jakarta.persistence.Id;
1819
import jakarta.persistence.ManyToOne;
1920
import jakarta.persistence.Tuple;
21+
import jakarta.persistence.criteria.CriteriaBuilder;
2022
import jakarta.persistence.criteria.CriteriaQuery;
2123
import jakarta.persistence.criteria.Fetch;
2224
import jakarta.persistence.criteria.From;
@@ -25,8 +27,6 @@
2527
import jakarta.persistence.criteria.Root;
2628
import jakarta.persistence.criteria.Selection;
2729

28-
import static org.junit.jupiter.api.Assertions.fail;
29-
3030
@Jpa(
3131
annotatedClasses = {
3232
IllegalArgumentExceptionTest.Person.class,
@@ -41,17 +41,16 @@ public void testCriteriaTupleQuerySameAlias(EntityManagerFactoryScope scope) {
4141
final CriteriaQuery<Tuple> query = scope.getEntityManagerFactory().getCriteriaBuilder().createTupleQuery();
4242
final Root<Person> person = query.from( Person.class );
4343

44-
try {
45-
List list = new ArrayList();
46-
list.add( person.get( "id" ).alias( "a" ) );
47-
list.add( person.get( "name" ).alias( "a" ) );
44+
Assertions.assertThrows(
45+
IllegalArgumentException.class,
46+
() -> {
47+
List list = new ArrayList();
48+
list.add( person.get( "id" ).alias( "a" ) );
49+
list.add( person.get( "name" ).alias( "a" ) );
4850

49-
query.multiselect( list );
50-
fail( "TCK expects an IllegalArgumentException" );
51-
}
52-
catch (IllegalArgumentException iae) {
53-
//expected by TCK
54-
}
51+
query.multiselect( list );
52+
}
53+
);
5554
}
5655

5756
@Test
@@ -65,13 +64,11 @@ public void testCriteriaTupleQuerySameAlias1(EntityManagerFactoryScope scope) {
6564
person.get( "name" ).alias( "a" )
6665
};
6766

68-
try {
69-
query.multiselect( selection );
70-
fail( "TCK expects an IllegalArgumentException" );
71-
}
72-
catch (IllegalArgumentException iae) {
73-
//expected by TCK
74-
}
67+
Assertions.assertThrows(
68+
IllegalArgumentException.class,
69+
() ->
70+
query.multiselect( selection )
71+
);
7572
}
7673

7774
@Test
@@ -80,16 +77,14 @@ public void testCriteriaTupleQueryNonExistingAttributeNames(EntityManagerFactory
8077
final CriteriaQuery<Tuple> query = scope.getEntityManagerFactory().getCriteriaBuilder().createTupleQuery();
8178
final Root<Person> person = query.from( Person.class );
8279

83-
try {
84-
query.multiselect(
85-
person.get( "not_existing_attribute_name" ).alias( "a1" ),
86-
person.get( "another_not_existing_attribute_name" ).alias( "a2" )
87-
);
88-
fail( "TCK expects an IllegalArgumentException" );
89-
}
90-
catch (IllegalArgumentException iae) {
91-
// expected
92-
}
80+
Assertions.assertThrows(
81+
IllegalArgumentException.class,
82+
() ->
83+
query.multiselect(
84+
person.get( "not_existing_attribute_name" ).alias( "a1" ),
85+
person.get( "another_not_existing_attribute_name" ).alias( "a2" )
86+
)
87+
);
9388
}
9489

9590
@Test
@@ -98,29 +93,27 @@ public void testCriteriaStringQuery(EntityManagerFactoryScope scope) {
9893
final CriteriaQuery<String> query = scope.getEntityManagerFactory()
9994
.getCriteriaBuilder()
10095
.createQuery( String.class );
101-
try {
102-
final Root<Person> person = query.from( Person.class );
103-
person.get( "not_existing_attribute_name" );
104-
105-
fail( "TCK expects an IllegalArgumentException" );
106-
}
107-
catch (IllegalArgumentException iae) {
108-
//expected by TCK
109-
}
96+
Assertions.assertThrows(
97+
IllegalArgumentException.class,
98+
() -> {
99+
final Root<Person> person = query.from( Person.class );
100+
person.get( "not_existing_attribute_name" );
101+
}
102+
103+
);
110104
}
111105

112106
@Test
113107
public void testGetStringNonExistingAttributeName(EntityManagerFactoryScope scope) {
114-
try {
115-
final CriteriaQuery<Person> query = scope.getEntityManagerFactory()
116-
.getCriteriaBuilder()
117-
.createQuery( Person.class );
118-
query.from( Person.class ).get( "not_existing_attribute_name" );
119-
fail( "TCK expects an IllegalArgumentException" );
120-
}
121-
catch (IllegalArgumentException iae) {
122-
//expected by TCK
123-
}
108+
Assertions.assertThrows(
109+
IllegalArgumentException.class,
110+
() -> {
111+
final CriteriaQuery<Person> query = scope.getEntityManagerFactory()
112+
.getCriteriaBuilder()
113+
.createQuery( Person.class );
114+
query.from( Person.class ).get( "not_existing_attribute_name" );
115+
}
116+
);
124117
}
125118

126119
@Test
@@ -129,13 +122,11 @@ public void testJoinANonExistingAttributeNameToAFrom(EntityManagerFactoryScope s
129122
.getCriteriaBuilder()
130123
.createQuery( Person.class );
131124
final From<Person, Person> customer = query.from( Person.class );
132-
try {
133-
customer.join( "not_existing_attribute_name" );
134-
fail( "TCK expects an IllegalArgumentException" );
135-
}
136-
catch (IllegalArgumentException iae) {
137-
//expected by TCK
138-
}
125+
Assertions.assertThrows(
126+
IllegalArgumentException.class,
127+
() ->
128+
customer.join( "not_existing_attribute_name" )
129+
);
139130
}
140131

141132
@Test
@@ -144,13 +135,11 @@ public void testJoinANonExistingAttributeNameToAFrom2(EntityManagerFactoryScope
144135
.getCriteriaBuilder()
145136
.createQuery( Person.class );
146137
final From<Person, Person> customer = query.from( Person.class );
147-
try {
148-
customer.join( "not_existing_attribute_name", JoinType.INNER );
149-
fail( "TCK expects an IllegalArgumentException" );
150-
}
151-
catch (IllegalArgumentException iae) {
152-
//expected by TCK
153-
}
138+
Assertions.assertThrows(
139+
IllegalArgumentException.class,
140+
() ->
141+
customer.join( "not_existing_attribute_name", JoinType.INNER )
142+
);
154143
}
155144

156145
@Test
@@ -161,13 +150,11 @@ public void testJoinANonExistingAttributeNameToAJoin(EntityManagerFactoryScope s
161150

162151
final Root<Person> customer = query.from( Person.class );
163152
final Join<Person, Address> address = customer.join( "address" );
164-
try {
165-
address.join( "not_existing_attribute_name" );
166-
fail( "TCK expects an IllegalArgumentException" );
167-
}
168-
catch (IllegalArgumentException iae) {
169-
//expected by TCK
170-
}
153+
Assertions.assertThrows(
154+
IllegalArgumentException.class,
155+
() ->
156+
address.join( "not_existing_attribute_name" )
157+
);
171158
}
172159

173160
@Test
@@ -178,13 +165,11 @@ public void testJoinANonExistingAttributeNameToAJoin2(EntityManagerFactoryScope
178165

179166
final Root<Person> customer = query.from( Person.class );
180167
final Join<Person, Address> address = customer.join( "address" );
181-
try {
182-
address.join( "not_existing_attribute_name", JoinType.INNER );
183-
fail( "TCK expects an IllegalArgumentException" );
184-
}
185-
catch (IllegalArgumentException iae) {
186-
//expected by TCK
187-
}
168+
Assertions.assertThrows(
169+
IllegalArgumentException.class,
170+
() ->
171+
address.join( "not_existing_attribute_name", JoinType.INNER )
172+
);
188173
}
189174

190175
@Test
@@ -196,13 +181,56 @@ public void fetchFetchStringIllegalArgumentExceptionTest(EntityManagerFactorySco
196181
final From<Person, Person> customer = query.from( Person.class );
197182
final Fetch f = customer.fetch( "address" );
198183

199-
try {
200-
f.fetch( "not_existing_attribute_name" );
201-
fail( "TCK expects an IllegalArgumentException" );
202-
}
203-
catch (IllegalArgumentException iae) {
204-
//expected by TCK
205-
}
184+
Assertions.assertThrows(
185+
IllegalArgumentException.class,
186+
() ->
187+
f.fetch( "not_existing_attribute_name" )
188+
);
189+
}
190+
191+
@Test
192+
public void testHqlQueryWithWrongSemantic(EntityManagerFactoryScope scope) {
193+
scope.inEntityManager(
194+
entityManager -> {
195+
Assertions.assertThrows(
196+
IllegalArgumentException.class,
197+
() ->
198+
entityManager.createQuery( "Seletc p" ).getResultList()
199+
);
200+
}
201+
);
202+
203+
}
204+
205+
@Test
206+
public void testCriteriaNullReturnType(EntityManagerFactoryScope scope) {
207+
scope.inEntityManager(
208+
entityManager -> {
209+
Assertions.assertThrows(
210+
IllegalArgumentException.class,
211+
() -> {
212+
CriteriaBuilder criteriaBuilder = scope.getEntityManagerFactory().getCriteriaBuilder();
213+
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery( null );
214+
entityManager.createQuery( criteriaQuery ).getResultList();
215+
}
216+
);
217+
}
218+
);
219+
}
220+
221+
@Test
222+
public void testQueryWrongReturnType(EntityManagerFactoryScope scope) {
223+
scope.inEntityManager(
224+
entityManager -> {
225+
Assertions.assertThrows(
226+
IllegalArgumentException.class,
227+
() -> {
228+
entityManager.createQuery( "select p from Peron p", Integer.class ).getResultList();
229+
}
230+
);
231+
}
232+
);
233+
206234
}
207235

208236
@Entity(name = "Person")

0 commit comments

Comments
 (0)