Skip to content

Commit 09ded4e

Browse files
committed
HHH-19383 - Add a few more tests and put them together
Signed-off-by: Jan Schatteman <[email protected]>
1 parent e4ba14a commit 09ded4e

File tree

2 files changed

+110
-29
lines changed

2 files changed

+110
-29
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
8+
import org.hibernate.orm.test.sql.hand.Person;
9+
import org.hibernate.query.NativeQuery;
10+
import org.hibernate.testing.orm.domain.StandardDomainModel;
11+
import org.hibernate.testing.orm.junit.DomainModel;
12+
import org.hibernate.testing.orm.junit.Jira;
13+
import org.hibernate.testing.orm.junit.SessionFactory;
14+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
15+
import org.hibernate.testing.orm.domain.library.Book;
16+
import org.hibernate.type.StandardBasicTypes;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
22+
23+
/**
24+
* @author Jan Schatteman
25+
*/
26+
@DomainModel(
27+
standardModels = StandardDomainModel.LIBRARY
28+
)
29+
@SessionFactory
30+
@Jira("https://hibernate.atlassian.net/browse/HHH-19383")
31+
public class NativeQueryResultCheckingTests {
32+
33+
@Test
34+
@Jira("https://hibernate.atlassian.net/browse/HHH-19376")
35+
public void testForHHH19376(SessionFactoryScope scope) {
36+
scope.inTransaction(
37+
session -> {
38+
String sql = "SELECT p.*, COUNT(*) OVER() AS total_count "
39+
+ "FROM Person p "
40+
+ "WHERE p.name ILIKE :name "
41+
+ "ORDER BY p.id";
42+
// Declare Person as result type
43+
NativeQuery<Person> query = session.createNativeQuery(sql, Person.class);
44+
query.setParameter("name", "Ga%");
45+
query.setMaxResults(2);
46+
query.setFirstResult(0);
47+
// Now mutate the result set mapping and verify an Exception is thrown
48+
assertThrows( IllegalArgumentException.class,
49+
() -> query.addScalar( "total_count", StandardBasicTypes.LONG).list() );
50+
}
51+
);
52+
}
53+
54+
@Test
55+
public void testMutateResultSetMapping1(SessionFactoryScope scope) {
56+
scope.inTransaction(
57+
session ->
58+
assertThrows( IllegalArgumentException.class,
59+
() -> session.createNativeQuery( "select title, isbn from Book", String.class )
60+
.addScalar( "title", String.class )
61+
.addScalar( "isbn", String.class )
62+
.getResultList()
63+
)
64+
);
65+
}
66+
67+
@Test
68+
public void testMutateResultSetMapping2(SessionFactoryScope scope) {
69+
scope.inTransaction(
70+
session ->
71+
assertThrows( IllegalArgumentException.class,
72+
() -> session.createNativeQuery( "select title, isbn from Book", Book.class )
73+
// this mapping doesn't have an appropriate constructor in Book, should throw error
74+
.addScalar( "title", String.class )
75+
.addScalar( "isbn", String.class )
76+
.getResultList()
77+
)
78+
);
79+
}
80+
81+
@Test
82+
public void testRecordWithPrimitiveField(SessionFactoryScope scope) {
83+
scope.inTransaction(
84+
session ->
85+
assertDoesNotThrow(
86+
() -> session.createNativeQuery( "select id, name from Person", Record.class)
87+
// map a Long onto a primitive long, shouldn't throw an exception
88+
.addScalar("id", Long.class)
89+
.addScalar("name", String.class)
90+
.getResultList()
91+
)
92+
);
93+
}
94+
95+
static class Record {
96+
long id;
97+
String name;
98+
public Record(long id, String name) {
99+
this.id = id;
100+
this.name = name;
101+
}
102+
long id() {
103+
return id;
104+
}
105+
String name() {
106+
return name;
107+
}
108+
}
109+
110+
}

hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/query/NativeSQLQueriesTest.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
import org.hibernate.query.NativeQuery;
3939
import org.hibernate.query.Query;
4040
import org.hibernate.query.ResultListTransformer;
41-
import org.hibernate.testing.orm.junit.Jira;
42-
import org.hibernate.testing.orm.junit.JiraGroup;
4341
import org.hibernate.transform.ResultTransformer;
4442
import org.hibernate.transform.Transformers;
4543
import org.hibernate.type.StandardBasicTypes;
@@ -61,7 +59,6 @@
6159
import static org.junit.jupiter.api.Assertions.assertEquals;
6260
import static org.junit.jupiter.api.Assertions.assertFalse;
6361
import static org.junit.jupiter.api.Assertions.assertNotNull;
64-
import static org.junit.jupiter.api.Assertions.assertThrows;
6562
import static org.junit.jupiter.api.Assertions.assertTrue;
6663
import static org.junit.jupiter.api.Assertions.fail;
6764

@@ -941,32 +938,6 @@ public void testAliasToBeanMap(SessionFactoryScope scope) {
941938
);
942939
}
943940

944-
@Test
945-
@JiraGroup(
946-
{
947-
@Jira("https://hibernate.atlassian.net/browse/HHH-19376"),
948-
@Jira("https://hibernate.atlassian.net/browse/HHH-19383")
949-
}
950-
)
951-
public void testMutateResultSetMapping(SessionFactoryScope scope) {
952-
scope.inTransaction(
953-
session -> {
954-
String sql = "SELECT p.*, COUNT(*) OVER() AS total_count "
955-
+ "FROM Person p "
956-
+ "WHERE p.name ILIKE :name "
957-
+ "ORDER BY p.id";
958-
// Declare Person as result type
959-
NativeQuery<Person> query = session.createNativeQuery(sql, Person.class);
960-
query.setParameter("name", "Ja%");
961-
query.setMaxResults(2);
962-
query.setFirstResult(0);
963-
// Now mutate the result set mapping and verify an Exception is thrown
964-
assertThrows( IllegalArgumentException.class,
965-
() -> query.addScalar( "total_count", StandardBasicTypes.LONG).list() );
966-
}
967-
);
968-
}
969-
970941
private String buildLongString(int size, char baseChar) {
971942
StringBuilder buff = new StringBuilder();
972943
for( int i = 0; i < size; i++ ) {

0 commit comments

Comments
 (0)