-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
CriteriaHQLAlignmentTest.java
380 lines (331 loc) · 13.8 KB
/
CriteriaHQLAlignmentTest.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* 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.test.hql;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import javax.persistence.PersistenceException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.exception.SQLGrammarException;
import org.hibernate.hql.internal.ast.QueryTranslatorImpl;
import org.hibernate.hql.internal.ast.tree.SelectClause;
import org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory;
import org.hibernate.hql.spi.QueryTranslator;
import org.hibernate.hql.spi.QueryTranslatorFactory;
import org.hibernate.type.BigDecimalType;
import org.hibernate.type.BigIntegerType;
import org.hibernate.type.DoubleType;
import org.hibernate.type.LongType;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/**
* Tests cases for ensuring alignment between HQL and Criteria behavior.
*
* @author Max Rydahl Andersen
*/
public class CriteriaHQLAlignmentTest extends QueryTranslatorTestCase {
private boolean initialVersion2SqlFlagValue;
@Before
public void setVersion2SqlFlag() {
initialVersion2SqlFlagValue = SelectClause.VERSION2_SQL;
SelectClause.VERSION2_SQL = true;
}
@After
public void resetVersion2SqlFlag() {
SelectClause.VERSION2_SQL = initialVersion2SqlFlagValue;
}
@Override
public String[] getMappings() {
return new String[] {
"hql/Animal.hbm.xml",
};
}
@Override
public boolean createSchema() {
return true; // needed for the Criteria return type test
}
@Override
public boolean rebuildSessionFactoryOnError() {
return true;
}
@Test
public void testHQLAggregationReturnType() {
// EJB3: COUNT returns Long
QueryTranslatorImpl translator = createNewQueryTranslator( "select count(*) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0] );
translator = createNewQueryTranslator( "select count(h.heightInches) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0] );
// MAX, MIN return the type of the state-field to which they are applied.
translator = createNewQueryTranslator( "select max(h.heightInches) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0] );
translator = createNewQueryTranslator( "select max(h.id) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0] );
// AVG returns Double.
translator = createNewQueryTranslator( "select avg(h.heightInches) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0] );
translator = createNewQueryTranslator( "select avg(h.id) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0] );
translator = createNewQueryTranslator( "select avg(h.bigIntegerValue) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0] );
// SUM returns Long when applied to state-fields of integral types (other than BigInteger);
translator = createNewQueryTranslator( "select sum(h.id) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0] );
translator = createNewQueryTranslator( "select sum(h.intValue) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0] );
// SUM returns Double when applied to state-fields of floating point types;
translator = createNewQueryTranslator( "select sum(h.heightInches) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0] );
translator = createNewQueryTranslator( "select sum(h.floatValue) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0] );
// SUM returns BigInteger when applied to state-fields of type BigInteger
translator = createNewQueryTranslator( "select sum(h.bigIntegerValue) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", BigIntegerType.INSTANCE, translator.getReturnTypes()[0] );
// SUM and BigDecimal when applied to state-fields of type BigDecimal.
translator = createNewQueryTranslator( "select sum(h.bigDecimalValue) from Human h" );
assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
assertEquals( "incorrect return type", BigDecimalType.INSTANCE, translator.getReturnTypes()[0] );
// special case to test classicquery special case handling of count(*)
String hql = "select count(*) from Human h";
QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
QueryTranslator oldQueryTranslator = classic.createQueryTranslator( hql, hql, Collections.EMPTY_MAP,
sessionFactory(), null );
oldQueryTranslator.compile( Collections.EMPTY_MAP, true);
assertEquals( "incorrect return type count", 1, oldQueryTranslator.getReturnTypes().length );
assertEquals( "incorrect return type", LongType.INSTANCE, oldQueryTranslator.getReturnTypes()[0] );
}
@Test
@TestForIssue( jiraKey = "HHH-1724" )
public void testCriteriaAggregationReturnType() {
Session s = openSession();
s.beginTransaction();
Human human = new Human();
human.setBigIntegerValue( new BigInteger("42") );
human.setBigDecimalValue( new BigDecimal(45) );
s.save(human);
s.flush();
s.clear();
// EJB3: COUNT returns Long
Long longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.rowCount()).uniqueResult();
assertEquals(longValue, new Long(1));
longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.count("heightInches")).uniqueResult();
assertEquals(longValue, new Long(1));
// MAX, MIN return the type of the state-field to which they are applied.
Double dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.max( "heightInches" )).uniqueResult();
assertNotNull(dblValue);
longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.max( "id" )).uniqueResult();
assertNotNull(longValue);
// AVG returns Double.
dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "heightInches" )).uniqueResult();
assertNotNull(dblValue);
dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "id" )).uniqueResult();
assertNotNull(dblValue);
dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "bigIntegerValue" )).uniqueResult();
assertNotNull(dblValue);
// SUM returns Long when applied to state-fields of integral types (other than BigInteger);
longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.sum( "id" )).uniqueResult();
assertNotNull(longValue);
longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.sum( "intValue" )).uniqueResult();
assertNotNull(longValue);
// SUM returns Double when applied to state-fields of floating point types;
dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.sum( "heightInches" )).uniqueResult();
assertNotNull(dblValue);
dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.sum( "floatValue" )).uniqueResult();
assertNotNull(dblValue);
// SUM returns BigInteger when applied to state-fields of type BigInteger
BigInteger bigIValue = (BigInteger) s.createCriteria( Human.class ).setProjection( Projections.sum( "bigIntegerValue" )).uniqueResult();
assertNotNull(bigIValue);
// SUM and BigDecimal when applied to state-fields of type BigDecimal.
BigDecimal bigDValue = (BigDecimal) s.createCriteria( Human.class ).setProjection( Projections.sum( "bigDecimalValue" )).uniqueResult();
assertNotNull(bigDValue);
s.delete( human );
s.flush();
s.getTransaction().commit();
s.close();
}
@Test
public void testCountReturnValues() {
Session s = openSession();
Transaction t = s.beginTransaction();
Human human1 = new Human();
human1.setName( new Name( "John", 'Q', "Public" ) );
human1.setNickName( "Johnny" );
s.save(human1);
Human human2 = new Human();
human2.setName( new Name( "John", 'A', "Doe" ) );
human2.setNickName( "Johnny" );
s.save( human2 );
Human human3 = new Human();
human3.setName( new Name( "John", 'A', "Doe" ) );
human3.setNickName( "Jack" );
s.save( human3 );
Human human4 = new Human();
human4.setName( new Name( "John", 'A', "Doe" ) );
s.save( human4 );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Long count = ( Long ) s.createQuery( "select count( * ) from Human" ).uniqueResult();
assertEquals( 4, count.longValue() );
s.clear();
count = ( Long ) s.createCriteria( Human.class )
.setProjection( Projections.rowCount() )
.uniqueResult();
assertEquals( 4, count.longValue() );
s.clear();
count = ( Long ) s.createQuery( "select count( nickName ) from Human" ).uniqueResult();
assertEquals( 3, count.longValue() );
s.clear();
count = ( Long ) s.createCriteria( Human.class )
.setProjection( Projections.count( "nickName" ) )
.uniqueResult();
assertEquals( 3, count.longValue() );
s.clear();
count = ( Long ) s.createQuery( "select count( distinct nickName ) from Human" ).uniqueResult();
assertEquals( 2, count.longValue() );
s.clear();
count = ( Long ) s.createCriteria( Human.class )
.setProjection( Projections.count( "nickName" ).setDistinct() )
.uniqueResult();
assertEquals( 2, count.longValue() );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
try {
count = ( Long ) s.createQuery( "select count( distinct name ) from Human" ).uniqueResult();
if ( ! getDialect().supportsTupleDistinctCounts() ) {
fail( "expected SQLGrammarException" );
}
assertEquals( 2, count.longValue() );
}
catch ( SQLGrammarException ex ) {
if ( ! getDialect().supportsTupleDistinctCounts() ) {
// expected
}
else {
throw ex;
}
}
finally {
t.rollback();
s.close();
}
s = openSession();
t = s.beginTransaction();
try {
count = ( Long ) s.createCriteria( Human.class )
.setProjection( Projections.count( "name" ).setDistinct() )
.uniqueResult();
if ( ! getDialect().supportsTupleDistinctCounts() ) {
fail( "expected SQLGrammarException" );
}
assertEquals( 2, count.longValue() );
}
catch ( SQLGrammarException ex ) {
if ( ! getDialect().supportsTupleDistinctCounts() ) {
// expected
}
else {
throw ex;
}
}
finally {
t.rollback();
s.close();
}
s = openSession();
t = s.beginTransaction();
count = ( Long ) s.createQuery( "select count( distinct name.first ) from Human" ).uniqueResult();
assertEquals( 1, count.longValue() );
s.clear();
count = ( Long ) s.createCriteria( Human.class )
.setProjection( Projections.count( "name.first" ).setDistinct() )
.uniqueResult();
assertEquals( 1, count.longValue() );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
try {
count = ( Long ) s.createQuery( "select count( name ) from Human" ).uniqueResult();
if ( ! getDialect().supportsTupleCounts() ) {
fail( "expected SQLGrammarException" );
}
assertEquals( 1, count.longValue() );
}
catch ( SQLGrammarException ex ) {
if ( ! getDialect().supportsTupleCounts() ) {
// expected
}
else {
throw ex;
}
}
catch (PersistenceException e) {
SQLGrammarException cause = assertTyping( SQLGrammarException.class, e.getCause() );
if ( ! getDialect().supportsTupleCounts() ) {
// expected
}
else {
throw e;
}
}
finally {
t.rollback();
s.close();
}
s = openSession();
t = s.beginTransaction();
try {
count = ( Long ) s.createCriteria( Human.class )
.setProjection( Projections.count( "name" ) )
.uniqueResult();
if ( ! getDialect().supportsTupleCounts() ) {
fail( "expected SQLGrammarException" );
}
assertEquals( 1, count.longValue() );
}
catch ( SQLGrammarException ex ) {
if ( ! getDialect().supportsTupleCounts() ) {
// expected
}
else {
throw ex;
}
}
finally {
t.rollback();
s.close();
}
s = openSession();
t = s.beginTransaction();
s.createQuery( "delete from Human" ).executeUpdate();
t.commit();
s.close();
}
}