/
ASTParserLoadingTest.java
3762 lines (3285 loc) · 132 KB
/
ASTParserLoadingTest.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.QueryException;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.TypeMismatchException;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.AbstractHANADialect;
import org.hibernate.dialect.CUBRIDDialect;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.IngresDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.SQLServer2008Dialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.Sybase11Dialect;
import org.hibernate.dialect.SybaseASE15Dialect;
import org.hibernate.dialect.SybaseAnywhereDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.dialect.TeradataDialect;
import org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.persister.entity.DiscriminatorType;
import org.hibernate.stat.QueryStatistics;
import org.hibernate.test.any.IntegerPropertyValue;
import org.hibernate.test.any.PropertySet;
import org.hibernate.test.any.PropertyValue;
import org.hibernate.test.any.StringPropertyValue;
import org.hibernate.test.cid.Customer;
import org.hibernate.test.cid.LineItem;
import org.hibernate.test.cid.LineItem.Id;
import org.hibernate.test.cid.Order;
import org.hibernate.test.cid.Product;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.transform.DistinctRootEntityResultTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.ComponentType;
import org.hibernate.type.ManyToOneType;
import org.hibernate.type.Type;
import org.jboss.logging.Logger;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.hibernate.testing.junit4.ExtraAssertions.assertClassAssignability;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Tests the integration of the new AST parser into the loading of query results using
* the Hibernate persisters and loaders.
* <p/>
* Also used to test the syntax of the resulting sql against the underlying
* database, specifically for functionality not supported by the classic
* parser.
*
* @author Steve
*/
@SkipForDialect(
value = CUBRIDDialect.class,
comment = "As of verion 8.4.1 CUBRID doesn't support temporary tables. This test fails with" +
"HibernateException: cannot doAfterTransactionCompletion multi-table deletes using dialect not supporting temp tables"
)
public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
private static final Logger log = Logger.getLogger( ASTParserLoadingTest.class );
private List<Long> createdAnimalIds = new ArrayList<Long>();
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Override
public String[] getMappings() {
return new String[] {
"hql/Animal.hbm.xml",
"hql/FooBarCopy.hbm.xml",
"hql/SimpleEntityWithAssociation.hbm.xml",
"hql/CrazyIdFieldNames.hbm.xml",
"hql/Image.hbm.xml",
"hql/ComponentContainer.hbm.xml",
"hql/VariousKeywordPropertyEntity.hbm.xml",
"hql/Constructor.hbm.xml",
"batchfetch/ProductLine.hbm.xml",
"cid/Customer.hbm.xml",
"cid/Order.hbm.xml",
"cid/LineItem.hbm.xml",
"cid/Product.hbm.xml",
"any/Properties.hbm.xml",
"legacy/Commento.hbm.xml",
"legacy/Marelo.hbm.xml"
};
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
Department.class,
Employee.class,
Title.class
};
}
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
cfg.setProperty( Environment.QUERY_TRANSLATOR, ASTQueryTranslatorFactory.class.getName() );
}
@Test
public void testSubSelectAsArithmeticOperand() {
Session s = openSession();
s.beginTransaction();
// first a control
s.createQuery( "from Zoo z where ( select count(*) from Zoo ) = 0" ).list();
// now as operands singly:
s.createQuery( "from Zoo z where ( select count(*) from Zoo ) + 0 = 0" ).list();
s.createQuery( "from Zoo z where 0 + ( select count(*) from Zoo ) = 0" ).list();
// and doubly:
s.createQuery( "from Zoo z where ( select count(*) from Zoo ) + ( select count(*) from Zoo ) = 0" ).list();
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-8432" )
public void testExpandListParameter() {
final Object[] namesArray = new Object[] {
"ZOO 1", "ZOO 2", "ZOO 3", "ZOO 4", "ZOO 5", "ZOO 6", "ZOO 7",
"ZOO 8", "ZOO 9", "ZOO 10", "ZOO 11", "ZOO 12"
};
final Object[] citiesArray = new Object[] {
"City 1", "City 2", "City 3", "City 4", "City 5", "City 6", "City 7",
"City 8", "City 9", "City 10", "City 11", "City 12"
};
Session session = openSession();
session.getTransaction().begin();
Address address = new Address();
Zoo zoo = new Zoo( "ZOO 1", address );
address.setCity( "City 1" );
session.save( zoo );
session.getTransaction().commit();
session.clear();
session.getTransaction().begin();
List result = session.createQuery( "FROM Zoo z WHERE z.name IN (?1) and z.address.city IN (?11)" )
.setParameterList( "1", namesArray )
.setParameterList( "11", citiesArray )
.list();
assertEquals( 1, result.size() );
session.getTransaction().commit();
session.clear();
session.getTransaction().begin();
zoo = (Zoo) session.get( Zoo.class, zoo.getId() );
session.delete( zoo );
session.getTransaction().commit();
session.close();
}
@Test
@TestForIssue(jiraKey = "HHH-8699")
// For now, restrict to H2. Selecting w/ predicate functions cause issues for too many dialects.
@RequiresDialect(value = H2Dialect.class, jiraKey = "HHH-9052")
public void testBooleanPredicate() {
final Session session = openSession();
session.getTransaction().begin();
final Constructor constructor = new Constructor();
session.save( constructor );
session.getTransaction().commit();
session.clear();
Constructor.resetConstructorExecutionCount();
session.getTransaction().begin();
final Constructor result = (Constructor) session.createQuery(
"select new Constructor( c.id, c.id is not null, c.id = c.id, c.id + 1, concat( c.id, 'foo' ) ) from Constructor c where c.id = :id"
).setParameter( "id", constructor.getId() ).uniqueResult();
session.getTransaction().commit();
assertEquals( 1, Constructor.getConstructorExecutionCount() );
assertEquals( new Constructor( constructor.getId(), true, true, constructor.getId() + 1, constructor.getId() + "foo" ), result );
session.close();
}
@Test
public void testJpaTypeOperator() {
// just checking syntax here...
Session s = openSession();
s.beginTransaction();
///////////////////////////////////////////////////////////////
// where clause
// control
s.createQuery( "from Animal a where a.class = Dog" ).list();
// test
s.createQuery( "from Animal a where type(a) = Dog" ).list();
///////////////////////////////////////////////////////////////
// select clause (at some point we should unify these)
// control
Query query = s.createQuery( "select a.class from Animal a where a.class = Dog" );
query.list(); // checks syntax
assertEquals( 1, query.getReturnTypes().length );
assertEquals( Integer.class, query.getReturnTypes()[0].getReturnedClass() ); // always integer for joined
// test
query = s.createQuery( "select type(a) from Animal a where type(a) = Dog" );
query.list(); // checks syntax
assertEquals( 1, query.getReturnTypes().length );
assertEquals( DiscriminatorType.class, query.getReturnTypes()[0].getClass() );
assertEquals( Class.class, query.getReturnTypes()[0].getReturnedClass() );
s.getTransaction().commit();
s.close();
}
@Test
public void testComponentJoins() {
Session s = openSession();
s.beginTransaction();
ComponentContainer root = new ComponentContainer(
new ComponentContainer.Address(
"123 Main",
"Anywhere",
"USA",
new ComponentContainer.Address.Zip( 12345, 6789 )
)
);
s.save( root );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
List result = s.createQuery( "select a from ComponentContainer c join c.address a" ).list();
assertEquals( 1, result.size() );
assertTrue( ComponentContainer.Address.class.isInstance( result.get( 0 ) ) );
result = s.createQuery( "select a.zip from ComponentContainer c join c.address a" ).list();
assertEquals( 1, result.size() );
assertTrue( ComponentContainer.Address.Zip.class.isInstance( result.get( 0 ) ) );
result = s.createQuery( "select z from ComponentContainer c join c.address a join a.zip z" ).list();
assertEquals( 1, result.size() );
assertTrue( ComponentContainer.Address.Zip.class.isInstance( result.get( 0 ) ) );
result = s.createQuery( "select z.code from ComponentContainer c join c.address a join a.zip z" ).list();
assertEquals( 1, result.size() );
assertTrue( Integer.class.isInstance( result.get( 0 ) ) );
s.delete( root );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9642")
public void testLazyAssociationInComponent() {
Session session = openSession();
session.getTransaction().begin();
Address address = new Address();
Zoo zoo = new Zoo( "ZOO 1", address );
address.setCity( "City 1" );
StateProvince stateProvince = new StateProvince();
stateProvince.setName( "Illinois" );
session.save( stateProvince );
address.setStateProvince( stateProvince );
session.save( zoo );
session.getTransaction().commit();
session.close();
session = openSession();
session.getTransaction().begin();
zoo = (Zoo) session.createQuery( "from Zoo z" ).uniqueResult();
assertNotNull( zoo );
assertNotNull( zoo.getAddress() );
assertEquals( "City 1", zoo.getAddress().getCity() );
assertFalse( Hibernate.isInitialized( zoo.getAddress().getStateProvince() ) );
assertEquals( "Illinois", zoo.getAddress().getStateProvince().getName() );
assertTrue( Hibernate.isInitialized( zoo.getAddress().getStateProvince() ) );
session.getTransaction().commit();
session.close();
session = openSession();
session.getTransaction().begin();
zoo = (Zoo) session.createQuery( "from Zoo z join fetch z.address.stateProvince" ).uniqueResult();
assertNotNull( zoo );
assertNotNull( zoo.getAddress() );
assertEquals( "City 1", zoo.getAddress().getCity() );
assertTrue( Hibernate.isInitialized( zoo.getAddress().getStateProvince() ) );
assertEquals( "Illinois", zoo.getAddress().getStateProvince().getName() );
session.getTransaction().commit();
session.close();
session = openSession();
session.getTransaction().begin();
zoo = (Zoo) session.createQuery( "from Zoo z join fetch z.address a join fetch a.stateProvince" ).uniqueResult();
assertNotNull( zoo );
assertNotNull( zoo.getAddress() );
assertEquals( "City 1", zoo.getAddress().getCity() );
assertTrue( Hibernate.isInitialized( zoo.getAddress().getStateProvince() ) );
assertEquals( "Illinois", zoo.getAddress().getStateProvince().getName() );
session.getTransaction().commit();
session.close();
session = openSession();
session.getTransaction().begin();
zoo.getAddress().setStateProvince( null );
session.delete( stateProvince );
session.delete( zoo );
session.getTransaction().commit();
session.close();
}
@Test
public void testJPAQLQualifiedIdentificationVariablesControl() {
// just checking syntax here...
Session s = openSession();
s.beginTransaction();
s.createQuery( "from VariousKeywordPropertyEntity where type = 'something'" ).list();
s.createQuery( "from VariousKeywordPropertyEntity where value = 'something'" ).list();
s.createQuery( "from VariousKeywordPropertyEntity where key = 'something'" ).list();
s.createQuery( "from VariousKeywordPropertyEntity where entry = 'something'" ).list();
s.createQuery( "from VariousKeywordPropertyEntity e where e.type = 'something'" ).list();
s.createQuery( "from VariousKeywordPropertyEntity e where e.value = 'something'" ).list();
s.createQuery( "from VariousKeywordPropertyEntity e where e.key = 'something'" ).list();
s.createQuery( "from VariousKeywordPropertyEntity e where e.entry = 'something'" ).list();
s.getTransaction().commit();
s.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testJPAQLMapKeyQualifier() {
Session s = openSession();
s.beginTransaction();
Human me = new Human();
me.setName( new Name( "Steve", null, "Ebersole" ) );
Human joe = new Human();
me.setName( new Name( "Joe", null, "Ebersole" ) );
me.setFamily( new HashMap() );
me.getFamily().put( "son", joe );
s.save( me );
s.save( joe );
s.getTransaction().commit();
s.close();
// in SELECT clause
{
// hibernate-only form
s = openSession();
s.beginTransaction();
List results = s.createQuery( "select distinct key(h.family) from Human h" ).list();
assertEquals( 1, results.size() );
Object key = results.get(0);
assertTrue( String.class.isAssignableFrom( key.getClass() ) );
s.getTransaction().commit();
s.close();
}
{
// jpa form
s = openSession();
s.beginTransaction();
List results = s.createQuery( "select distinct KEY(f) from Human h join h.family f" ).list();
assertEquals( 1, results.size() );
Object key = results.get(0);
assertTrue( String.class.isAssignableFrom( key.getClass() ) );
s.getTransaction().commit();
s.close();
}
// in WHERE clause
{
// hibernate-only form
s = openSession();
s.beginTransaction();
Long count = (Long) s.createQuery( "select count(*) from Human h where KEY(h.family) = 'son'" ).uniqueResult();
assertEquals( (Long)1L, count );
s.getTransaction().commit();
s.close();
}
{
// jpa form
s = openSession();
s.beginTransaction();
Long count = (Long) s.createQuery( "select count(*) from Human h join h.family f where key(f) = 'son'" ).uniqueResult();
assertEquals( (Long)1L, count );
s.getTransaction().commit();
s.close();
}
s = openSession();
s.beginTransaction();
s.delete( me );
s.delete( joe );
s.getTransaction().commit();
s.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testJPAQLMapEntryQualifier() {
Session s = openSession();
s.beginTransaction();
Human me = new Human();
me.setName( new Name( "Steve", null, "Ebersole" ) );
Human joe = new Human();
me.setName( new Name( "Joe", null, "Ebersole" ) );
me.setFamily( new HashMap() );
me.getFamily().put( "son", joe );
s.save( me );
s.save( joe );
s.getTransaction().commit();
s.close();
// in SELECT clause
{
// hibernate-only form
s = openSession();
s.beginTransaction();
List results = s.createQuery( "select entry(h.family) from Human h" ).list();
assertEquals( 1, results.size() );
Object result = results.get(0);
assertTrue( Map.Entry.class.isAssignableFrom( result.getClass() ) );
Map.Entry entry = (Map.Entry) result;
assertTrue( String.class.isAssignableFrom( entry.getKey().getClass() ) );
assertTrue( Human.class.isAssignableFrom( entry.getValue().getClass() ) );
s.getTransaction().commit();
s.close();
}
{
// jpa form
s = openSession();
s.beginTransaction();
List results = s.createQuery( "select ENTRY(f) from Human h join h.family f" ).list();
assertEquals( 1, results.size() );
Object result = results.get(0);
assertTrue( Map.Entry.class.isAssignableFrom( result.getClass() ) );
Map.Entry entry = (Map.Entry) result;
assertTrue( String.class.isAssignableFrom( entry.getKey().getClass() ) );
assertTrue( Human.class.isAssignableFrom( entry.getValue().getClass() ) );
s.getTransaction().commit();
s.close();
}
// not exactly sure of the syntax of ENTRY in the WHERE clause...
s = openSession();
s.beginTransaction();
s.delete( me );
s.delete( joe );
s.getTransaction().commit();
s.close();
}
@Test
@SuppressWarnings( {"unchecked"})
public void testJPAQLMapValueQualifier() {
Session s = openSession();
s.beginTransaction();
Human me = new Human();
me.setName( new Name( "Steve", null, "Ebersole" ) );
Human joe = new Human();
me.setName( new Name( "Joe", null, "Ebersole" ) );
me.setFamily( new HashMap() );
me.getFamily().put( "son", joe );
s.save( me );
s.save( joe );
s.getTransaction().commit();
s.close();
// in SELECT clause
{
// hibernate-only form
s = openSession();
s.beginTransaction();
List results = s.createQuery( "select value(h.family) from Human h" ).list();
assertEquals( 1, results.size() );
Object result = results.get(0);
assertTrue( Human.class.isAssignableFrom( result.getClass() ) );
s.getTransaction().commit();
s.close();
}
{
// jpa form
s = openSession();
s.beginTransaction();
List results = s.createQuery( "select VALUE(f) from Human h join h.family f" ).list();
assertEquals( 1, results.size() );
Object result = results.get(0);
assertTrue( Human.class.isAssignableFrom( result.getClass() ) );
s.getTransaction().commit();
s.close();
}
// in WHERE clause
{
// hibernate-only form
s = openSession();
s.beginTransaction();
Long count = (Long) s.createQuery( "select count(*) from Human h where VALUE(h.family) = :joe" ).setParameter( "joe", joe ).uniqueResult();
// ACTUALLY EXACTLY THE SAME AS:
// select count(*) from Human h where h.family = :joe
assertEquals( (Long)1L, count );
s.getTransaction().commit();
s.close();
}
{
// jpa form
s = openSession();
s.beginTransaction();
Long count = (Long) s.createQuery( "select count(*) from Human h join h.family f where value(f) = :joe" ).setParameter( "joe", joe ).uniqueResult();
// ACTUALLY EXACTLY THE SAME AS:
// select count(*) from Human h join h.family f where f = :joe
assertEquals( (Long)1L, count );
s.getTransaction().commit();
s.close();
}
s = openSession();
s.beginTransaction();
s.delete( me );
s.delete( joe );
s.getTransaction().commit();
s.close();
}
@Test
@SkipForDialect(
value = IngresDialect.class,
jiraKey = "HHH-4961",
comment = "Ingres does not support this scoping in 9.3"
)
public void testPaginationWithPolymorphicQuery() {
Session s = openSession();
s.beginTransaction();
Human h = new Human();
h.setName( new Name( "Steve", null, "Ebersole" ) );
s.save( h );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
List results = s.createQuery( "from java.lang.Object" ).setMaxResults( 2 ).list();
assertEquals( 1, results.size() );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.delete( h );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-2045" )
@RequiresDialect( H2Dialect.class )
public void testEmptyInList() {
Session session = openSession();
session.beginTransaction();
Human human = new Human();
human.setName( new Name( "Lukasz", null, "Antoniak" ) );
human.setNickName( "NONE" );
session.save( human );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Human h where h.nickName in ()" ).list();
assertEquals( 0, results.size() );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
session.delete( human );
session.getTransaction().commit();
session.close();
}
@Test
@TestForIssue( jiraKey = "HHH-2851")
public void testMultipleRefsToSameParam() {
Session s = openSession();
s.beginTransaction();
Human h = new Human();
h.setName( new Name( "Johnny", 'B', "Goode" ) );
s.save( h );
h = new Human();
h.setName( new Name( "Steve", null, "Ebersole" ) );
s.save( h );
h = new Human();
h.setName( new Name( "Bono", null, null ) );
s.save( h );
h = new Human();
h.setName( new Name( "Steve", 'Z', "Johnny" ) );
h.setIntValue( 1 );
s.save( h );
h = new Human();
h.setName( new Name( null, null, null ) );
s.save( h );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
List results = s.createQuery( "from Human where name.first = :name or name.last=:name" )
.setParameter( "name", "Johnny" )
.list();
assertEquals( 2, results.size() );
results = s.createQuery( "from Human where name.last = :name or :name is null" )
.setParameter( "name", "Goode" )
.list();
assertEquals( 1, results.size() );
results = s.createQuery( "from Human where :name is null or name.last = :name" )
.setParameter( "name", "Goode" )
.list();
assertEquals( 1, results.size() );
results = s.createQuery( "from Human where name.first = :firstName and (name.last = :name or :name is null)" )
.setParameter( "firstName", "Bono" )
.setParameter( "name", null )
.list();
assertEquals( 1, results.size() );
results = s.createQuery( "from Human where name.first = :firstName and ( :name is null or name.last = :name )" )
.setParameter( "firstName", "Bono" )
.setParameter( "name", null )
.list();
assertEquals( 1, results.size() );
results = s.createQuery( "from Human where intValue = :intVal or :intVal is null" )
.setParameter( "intVal", 1 )
.list();
assertEquals( 1, results.size() );
results = s.createQuery( "from Human where :intVal is null or intValue = :intVal" )
.setParameter( "intVal", 1 )
.list();
assertEquals( 1, results.size() );
results = s.createQuery( "from Human where intValue = :intVal or :intVal is null" )
.setParameter( "intVal", null )
.list();
assertEquals( 5, results.size() );
results = s.createQuery( "from Human where :intVal is null or intValue = :intVal" )
.setParameter( "intVal", null )
.list();
assertEquals( 5, results.size() );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery( "delete Human" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
@Test
public void testComponentNullnessChecks() {
Session s = openSession();
s.beginTransaction();
Human h = new Human();
h.setName( new Name( "Johnny", 'B', "Goode" ) );
s.save( h );
h = new Human();
h.setName( new Name( "Steve", null, "Ebersole" ) );
s.save( h );
h = new Human();
h.setName( new Name( "Bono", null, null ) );
s.save( h );
h = new Human();
h.setName( new Name( null, null, null ) );
s.save( h );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
List results = s.createQuery( "from Human where name is null" ).list();
assertEquals( 1, results.size() );
results = s.createQuery( "from Human where name is not null" ).list();
assertEquals( 3, results.size() );
String query =
( getDialect() instanceof DB2Dialect || getDialect() instanceof HSQLDialect ) ?
"from Human where cast(? as string) is null" :
"from Human where ? is null"
;
s.createQuery( query ).setParameter( 0, null ).list();
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery( "delete Human" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-4150" )
public void testSelectClauseCaseWithSum() {
Session s = openSession();
Transaction t = s.beginTransaction();
Human h1 = new Human();
h1.setBodyWeight( 74.0f );
h1.setDescription( "Me" );
s.persist( h1 );
Human h2 = new Human();
h2.setBodyWeight( 125.0f );
h2.setDescription( "big persion #1" );
s.persist( h2 );
Human h3 = new Human();
h3.setBodyWeight( 110.0f );
h3.setDescription( "big persion #2" );
s.persist( h3 );
s.flush();
Number count = (Number) s.createQuery( "select sum(case when bodyWeight > 100 then 1 else 0 end) from Human" ).uniqueResult();
assertEquals( 2, count.intValue() );
count = (Number) s.createQuery( "select sum(case when bodyWeight > 100 then bodyWeight else 0 end) from Human" ).uniqueResult();
assertEquals( h2.getBodyWeight() + h3.getBodyWeight(), count.floatValue(), 0.001 );
t.rollback();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-4150" )
public void testSelectClauseCaseWithCountDistinct() {
Session s = openSession();
Transaction t = s.beginTransaction();
Human h1 = new Human();
h1.setBodyWeight( 74.0f );
h1.setDescription( "Me" );
h1.setNickName( "Oney" );
s.persist( h1 );
Human h2 = new Human();
h2.setBodyWeight( 125.0f );
h2.setDescription( "big persion" );
h2.setNickName( "big #1" );
s.persist( h2 );
Human h3 = new Human();
h3.setBodyWeight( 110.0f );
h3.setDescription( "big persion" );
h3.setNickName( "big #2" );
s.persist( h3 );
s.flush();
Number count = (Number) s.createQuery( "select count(distinct case when bodyWeight > 100 then description else null end) from Human" ).uniqueResult();
assertEquals( 1, count.intValue() );
count = (Number) s.createQuery( "select count(case when bodyWeight > 100 then description else null end) from Human" ).uniqueResult();
assertEquals( 2, count.intValue() );
count = (Number) s.createQuery( "select count(distinct case when bodyWeight > 100 then nickName else null end) from Human" ).uniqueResult();
assertEquals( 2, count.intValue() );
t.rollback();
s.close();
}
@Test
public void testInvalidCollectionDereferencesFail() {
Session s = openSession();
s.beginTransaction();
// control group...
s.createQuery( "from Animal a join a.offspring o where o.description = 'xyz'" ).list();
s.createQuery( "from Animal a join a.offspring o where o.father.description = 'xyz'" ).list();
s.createQuery( "from Animal a join a.offspring o order by o.description" ).list();
s.createQuery( "from Animal a join a.offspring o order by o.father.description" ).list();
try {
s.createQuery( "from Animal a where a.offspring.description = 'xyz'" ).list();
fail( "illegal collection dereference semantic did not cause failure" );
}
catch( QueryException qe ) {
log.trace("expected failure...", qe);
}
try {
s.createQuery( "from Animal a where a.offspring.father.description = 'xyz'" ).list();
fail( "illegal collection dereference semantic did not cause failure" );
}
catch( QueryException qe ) {
log.trace("expected failure...", qe);
}
try {
s.createQuery( "from Animal a order by a.offspring.description" ).list();
fail( "illegal collection dereference semantic did not cause failure" );
}
catch( QueryException qe ) {
log.trace("expected failure...", qe);
}
try {
s.createQuery( "from Animal a order by a.offspring.father.description" ).list();
fail( "illegal collection dereference semantic did not cause failure" );
}
catch( QueryException qe ) {
log.trace("expected failure...", qe);
}
s.getTransaction().commit();
s.close();
}
@Test
public void testConcatenation() {
// simple syntax checking...
Session s = openSession();
s.beginTransaction();
s.createQuery( "from Human h where h.nickName = '1' || 'ov' || 'tha' || 'few'" ).list();
s.getTransaction().commit();
s.close();
}
@Test
public void testExpressionWithParamInFunction() {
Session s = openSession();
s.beginTransaction();
s.createQuery( "from Animal a where abs(a.bodyWeight-:param) < 2.0" ).setLong( "param", 1 ).list();
s.createQuery( "from Animal a where abs(:param - a.bodyWeight) < 2.0" ).setLong( "param", 1 ).list();
if ( ( getDialect() instanceof HSQLDialect ) || ( getDialect() instanceof DB2Dialect ) ) {
// HSQLDB and DB2 don't like the abs(? - ?) syntax. bit work if at least one parameter is typed...
s.createQuery( "from Animal where abs(cast(:x as long) - :y) < 2.0" ).setLong( "x", 1 ).setLong( "y", 1 ).list();
s.createQuery( "from Animal where abs(:x - cast(:y as long)) < 2.0" ).setLong( "x", 1 ).setLong( "y", 1 ).list();
s.createQuery( "from Animal where abs(cast(:x as long) - cast(:y as long)) < 2.0" ).setLong( "x", 1 ).setLong( "y", 1 ).list();
}
else {
s.createQuery( "from Animal where abs(:x - :y) < 2.0" ).setLong( "x", 1 ).setLong( "y", 1 ).list();
}
if ( getDialect() instanceof DB2Dialect ) {
s.createQuery( "from Animal where lower(upper(cast(:foo as string))) like 'f%'" ).setString( "foo", "foo" ).list();
}
else {
s.createQuery( "from Animal where lower(upper(:foo)) like 'f%'" ).setString( "foo", "foo" ).list();
}
s.createQuery( "from Animal a where abs(abs(a.bodyWeight - 1.0 + :param) * abs(length('ffobar')-3)) = 3.0" ).setLong(
"param", 1
).list();
if ( getDialect() instanceof DB2Dialect ) {
s.createQuery( "from Animal where lower(upper('foo') || upper(cast(:bar as string))) like 'f%'" ).setString( "bar", "xyz" ).list();
}
else {
s.createQuery( "from Animal where lower(upper('foo') || upper(:bar)) like 'f%'" ).setString( "bar", "xyz" ).list();
}
if ( getDialect() instanceof AbstractHANADialect ) {
s.createQuery( "from Animal where abs(cast(1 as double) - cast(:param as double)) = 1.0" )
.setLong( "param", 1 ).list();
}
else if ( !( getDialect() instanceof PostgreSQLDialect || getDialect() instanceof PostgreSQL81Dialect
|| getDialect() instanceof MySQLDialect ) ) {
s.createQuery( "from Animal where abs(cast(1 as float) - cast(:param as float)) = 1.0" )
.setLong( "param", 1 ).list();
}
s.getTransaction().commit();
s.close();
}
@Test
public void testCrazyIdFieldNames() {
MoreCrazyIdFieldNameStuffEntity top = new MoreCrazyIdFieldNameStuffEntity( "top" );
HeresAnotherCrazyIdFieldName next = new HeresAnotherCrazyIdFieldName( "next" );
top.setHeresAnotherCrazyIdFieldName( next );
MoreCrazyIdFieldNameStuffEntity other = new MoreCrazyIdFieldNameStuffEntity( "other" );
Session s = openSession();
s.beginTransaction();
s.save( next );
s.save( top );
s.save( other );
s.flush();
List results = s.createQuery( "select e.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e where e.heresAnotherCrazyIdFieldName is not null" ).list();
assertEquals( 1, results.size() );
Object result = results.get( 0 );
assertClassAssignability( HeresAnotherCrazyIdFieldName.class, result.getClass() );
assertSame( next, result );
results = s.createQuery( "select e.heresAnotherCrazyIdFieldName.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e where e.heresAnotherCrazyIdFieldName is not null" ).list();
assertEquals( 1, results.size() );
result = results.get( 0 );
assertClassAssignability( Long.class, result.getClass() );
assertEquals( next.getHeresAnotherCrazyIdFieldName(), result );
results = s.createQuery( "select e.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e" ).list();
assertEquals( 1, results.size() );
Iterator itr = s.createQuery( "select e.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e" ).iterate();
assertTrue( itr.hasNext() ); itr.next(); assertFalse( itr.hasNext() );
s.delete( top );
s.delete( next );
s.delete( other );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-2257" )
public void testImplicitJoinsInDifferentClauses() {
// both the classic and ast translators output the same syntactically valid sql
// for all of these cases; the issue is that shallow (iterate) and
// non-shallow (list/scroll) queries return different results because the
// shallow skips the inner join which "weeds out" results from the non-shallow queries.
// The results were initially different depending upon the clause(s) in which the
// implicit join occurred
Session s = openSession();
s.beginTransaction();
SimpleEntityWithAssociation owner = new SimpleEntityWithAssociation( "owner" );
SimpleAssociatedEntity e1 = new SimpleAssociatedEntity( "thing one", owner );
SimpleAssociatedEntity e2 = new SimpleAssociatedEntity( "thing two" );
s.save( e1 );
s.save( e2 );
s.save( owner );
s.getTransaction().commit();
s.close();
checkCounts( "select e.owner from SimpleAssociatedEntity e", 1, "implicit-join in select clause" );
checkCounts( "select e.id, e.owner from SimpleAssociatedEntity e", 1, "implicit-join in select clause" );