/
NativeQueryOrdinalParametersTest.java
246 lines (198 loc) · 6.23 KB
/
NativeQueryOrdinalParametersTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.query;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Query;
import javax.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.hibernate.Session;
import org.hibernate.dialect.PostgreSQL82Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.spi.NativeQueryImplementor;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Andrea Boriero
*/
public class NativeQueryOrdinalParametersTest extends BaseEntityManagerFunctionalTestCase {
private static final String[] GAME_TITLES = { "Super Mario Brothers", "Mario Kart", "F-Zero" };
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
Game.class,
Node.class
};
}
@Before
public void setUp() {
doInJPA( this::entityManagerFactory, entityManager -> {
for ( String title : GAME_TITLES ) {
Game game = new Game( title );
entityManager.persist( game );
}
} );
}
@After
public void tearDown() {
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.createQuery( "delete from Game" ).executeUpdate();
} );
}
@Test
@TestForIssue(jiraKey = "HHH-10885")
public void testNativeQueryIndexedOrdinalParameter() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query query = entityManager.createNativeQuery( "SELECT * FROM GAME g WHERE title = ?1" );
query.setParameter( 1, "Super Mario Brothers" );
List list = query.getResultList();
assertEquals( 1, list.size() );
} );
}
@Test
@TestForIssue(jiraKey = "HHH-10885")
public void testNativeQueryOrdinalParameter() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query query = entityManager.createNativeQuery( "SELECT * FROM GAME g WHERE title = ?" );
query.setParameter( 1, "Super Mario Brothers" );
List list = query.getResultList();
assertEquals( 1, list.size() );
} );
}
@Test
@TestForIssue(jiraKey = "HHH-11121")
public void testConflictWithSessionNativeQuery() {
final String sqlString = "SELECT * FROM GAME g WHERE title = ?";
doInJPA( this::entityManagerFactory, entityManager -> {
NativeQuery sqlQuery = entityManager.unwrap( Session.class ).createSQLQuery( sqlString );
sqlQuery.setString( 1, "Super Mario Brothers" ).setCacheable( true );
List results = sqlQuery.list();
assertEquals( 1, results.size() );
NativeQueryImplementor query = (NativeQueryImplementor) entityManager.createNativeQuery( sqlString );
query.setString( 1, "Super Mario Brothers" );
List list = query.list();
assertEquals( 1, list.size() );
sqlQuery = entityManager.unwrap( Session.class ).createSQLQuery( sqlString );
sqlQuery.setString( 1, "Super Mario Brothers" ).setCacheable( true );
results = sqlQuery.list();
assertEquals( 1, results.size() );
query.setString( 1, "Super Mario Brothers" );
} );
}
@Test
@TestForIssue(jiraKey = "HHH-12532")
@RequiresDialect(PostgreSQL82Dialect.class)
public void testCteNativeQueryOrdinalParameter() {
Node root1 = new Node();
root1.setCode( "ABC" );
Node root2 = new Node();
root2.setCode( "DEF" );
Node node11 = new Node();
node11.setParent( root1 );
Node node21 = new Node();
node21.setParent( root2 );
Node node211 = new Node();
node211.setParent( node21 );
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.persist( root1 );
entityManager.persist( root2 );
entityManager.persist( node11 );
entityManager.persist( node21 );
entityManager.persist( node211 );
} );
doInJPA( this::entityManagerFactory, entityManager -> {
Query cte = entityManager.createNativeQuery(
"WITH RECURSIVE CTE(id, parent_id) AS ( " +
" SELECT id, parent_id " +
" FROM Node " +
" WHERE code like ?1 and parent_id is null" +
" UNION ALL " +
" SELECT child.id, child.parent_id " +
" FROM Node child " +
" JOIN CTE cte " +
" ON cte.id = child.parent_id " +
") SELECT DISTINCT id as integer " +
" FROM CTE cte" );
List<Long> root1Ids = cte.setParameter( 1, "AB%" ).getResultList();
assertEquals( 2, root1Ids.size() );
assertTrue( root1Ids.contains( root1.getId() ) );
assertTrue( root1Ids.contains( node11.getId() ) );
List<Long> root2Ids = cte.setParameter( 1, "DE%" ).getResultList();
assertEquals( 3, root2Ids.size() );
assertTrue( root2Ids.contains( root2.getId() ) );
assertTrue( root2Ids.contains( node21.getId() ) );
assertTrue( root2Ids.contains( node211.getId() ) );
} );
}
@Entity(name = "Game")
@Table(name = "GAME")
public static class Game {
private Long id;
private String title;
public Game() {
}
public Game(String title) {
this.title = title;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NotNull
@Size(min = 3, max = 50)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
@Entity(name = "Node")
@Table(name = "Node")
public static class Node {
@Id
@GeneratedValue
private Integer id;
private String code;
@ManyToOne(fetch = FetchType.LAZY)
private Node parent;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
}
}