Skip to content

Commit 9a1bf3b

Browse files
jrenaatgavinking
authored andcommitted
HHH-19383 - Add a few more tests and put them together
Signed-off-by: Jan Schatteman <[email protected]>
1 parent a9461d1 commit 9a1bf3b

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.query.sql;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import jakarta.persistence.Tuple;
11+
import org.hibernate.orm.test.sql.hand.Person;
12+
import org.hibernate.query.NativeQuery;
13+
import org.hibernate.testing.orm.domain.StandardDomainModel;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.Jira;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.hibernate.testing.orm.domain.library.Book;
19+
import org.hibernate.type.StandardBasicTypes;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25+
26+
/**
27+
* @author Jan Schatteman
28+
*/
29+
@DomainModel(
30+
standardModels = StandardDomainModel.LIBRARY
31+
)
32+
@SessionFactory
33+
@Jira("https://hibernate.atlassian.net/browse/HHH-19383")
34+
public class NativeQueryResultCheckingTests {
35+
36+
@Test
37+
@Jira("https://hibernate.atlassian.net/browse/HHH-19376")
38+
public void testForHHH19376(SessionFactoryScope scope) {
39+
scope.inTransaction(
40+
session -> {
41+
String sql = "SELECT p.*, COUNT(*) OVER() AS total_count "
42+
+ "FROM Person p "
43+
+ "WHERE p.name ILIKE :name "
44+
+ "ORDER BY p.id";
45+
// Declare Person as result type
46+
NativeQuery<Person> query = session.createNativeQuery(sql, Person.class);
47+
query.setParameter("name", "Ga%");
48+
query.setMaxResults(2);
49+
query.setFirstResult(0);
50+
// Now mutate the result set mapping and verify an Exception is thrown
51+
assertThrows( IllegalArgumentException.class,
52+
() -> query.addScalar( "total_count", StandardBasicTypes.LONG).list() );
53+
}
54+
);
55+
}
56+
57+
@Test
58+
public void testOkMutateResultSetMappingWithString(SessionFactoryScope scope) {
59+
scope.inTransaction(
60+
session ->
61+
assertDoesNotThrow(
62+
() -> session.createNativeQuery( "select isbn from Book", String.class )
63+
.addScalar( "isbn", String.class )
64+
.getResultList()
65+
)
66+
);
67+
}
68+
69+
@Test
70+
public void testNokMutateResultSetMappingWithString(SessionFactoryScope scope) {
71+
scope.inTransaction(
72+
session ->
73+
assertThrows( IllegalArgumentException.class,
74+
() -> session.createNativeQuery( "select title, isbn from Book", String.class )
75+
.addScalar( "title", String.class )
76+
.addScalar( "isbn", String.class )
77+
.getResultList()
78+
)
79+
);
80+
}
81+
82+
@Test
83+
public void testOkMutateResultSetMappingWithBook(SessionFactoryScope scope) {
84+
scope.inTransaction(
85+
session ->
86+
assertDoesNotThrow(
87+
() -> session.createNativeQuery( "select id, name from Book", Book.class )
88+
.addScalar( "id", Integer.class )
89+
.addScalar( "name", String.class )
90+
.getResultList()
91+
)
92+
);
93+
}
94+
95+
@Test
96+
public void testNokMutateResultSetMappingWithBook(SessionFactoryScope scope) {
97+
scope.inTransaction(
98+
session ->
99+
assertThrows( IllegalArgumentException.class,
100+
() -> session.createNativeQuery( "select title, isbn from Book", Book.class )
101+
// this mapping doesn't have an appropriate constructor in Book, should throw error
102+
.addScalar( "title", String.class )
103+
.addScalar( "isbn", String.class )
104+
.getResultList()
105+
)
106+
);
107+
}
108+
109+
@Test
110+
public void testMutateResultSetMappingWithObjectArray(SessionFactoryScope scope) {
111+
scope.inTransaction(
112+
session ->
113+
assertDoesNotThrow(
114+
() -> {
115+
session.createNativeQuery( "select * from Book", Object[].class )
116+
.addScalar( "id", Integer.class )
117+
.addScalar( "name", String.class )
118+
.getResultList();
119+
}
120+
)
121+
);
122+
}
123+
124+
@Test
125+
public void testMutateResultSetMappingWithTuple(SessionFactoryScope scope) {
126+
scope.inTransaction(
127+
session ->
128+
assertDoesNotThrow(
129+
() -> {
130+
session.createNativeQuery( "select * from Book", Tuple.class )
131+
.addScalar( "id", Integer.class )
132+
.addScalar( "name", String.class )
133+
.getResultList();
134+
}
135+
)
136+
);
137+
}
138+
139+
@Test
140+
public void testMutateResultSetMappingWithMap(SessionFactoryScope scope) {
141+
scope.inTransaction(
142+
session ->
143+
assertDoesNotThrow(
144+
() -> {
145+
session.createNativeQuery( "select * from Book", Map.class )
146+
.addScalar( "id", Integer.class )
147+
.addScalar( "name", String.class )
148+
.getResultList();
149+
}
150+
)
151+
);
152+
}
153+
154+
@Test
155+
public void testMutateResultSetMappingWithList(SessionFactoryScope scope) {
156+
scope.inTransaction(
157+
session ->
158+
assertDoesNotThrow(
159+
() -> {
160+
session.createNativeQuery( "select * from Book", List.class )
161+
.addScalar( "id", Integer.class )
162+
.addScalar( "name", String.class )
163+
.getResultList();
164+
}
165+
)
166+
);
167+
}
168+
169+
@Test
170+
public void testRecordWithPrimitiveField(SessionFactoryScope scope) {
171+
scope.inTransaction(
172+
session ->
173+
assertDoesNotThrow(
174+
() -> session.createNativeQuery( "select id, name from Person", Record.class)
175+
// map a Long onto a primitive long, shouldn't throw an exception
176+
.addScalar("id", Long.class)
177+
.addScalar("name", String.class)
178+
.getResultList()
179+
)
180+
);
181+
}
182+
183+
static class Record {
184+
long id;
185+
String name;
186+
public Record(long id, String name) {
187+
this.id = id;
188+
this.name = name;
189+
}
190+
long id() {
191+
return id;
192+
}
193+
String name() {
194+
return name;
195+
}
196+
}
197+
198+
}

0 commit comments

Comments
 (0)