4343import jakarta .persistence .criteria .Root ;
4444
4545import static org .junit .jupiter .api .Assertions .assertEquals ;
46+ import static org .junit .jupiter .api .Assertions .assertTrue ;
4647import static org .junit .jupiter .api .Assertions .fail ;
4748
4849/**
@@ -72,10 +73,17 @@ public void testForHHH17967(SessionFactoryScope scope) {
7273 JpaCriteriaQuery <Contract > cq = cb .createQuery ( Contract .class );
7374 Root <Contract > root = cq .from ( Contract .class );
7475 cq .select ( root );
75- TypedQuery <Long > query = session .createQuery ( cq .createCountQuery () );
76+ TypedQuery <Long > countQuery = session .createQuery ( cq .createCountQuery () );
7677 try {
7778 // Leads to NPE on pre-6.5 versions
78- query .getSingleResult ();
79+ countQuery .getSingleResult ();
80+ }
81+ catch (Exception e ) {
82+ fail ( e );
83+ }
84+ TypedQuery <Boolean > existsQuery = session .createQuery ( cq .createExistsQuery () );
85+ try {
86+ existsQuery .getSingleResult ();
7987 }
8088 catch (Exception e ) {
8189 fail ( e );
@@ -95,10 +103,17 @@ public void testForHHH18850(SessionFactoryScope scope) {
95103 Root <Contract > root = cq .from ( Contract .class );
96104 cq .select ( root );
97105 cq .orderBy ( cb .asc ( root .get ( "customerName" ) ) );
98- TypedQuery <Long > query = session .createQuery ( cq .createCountQuery () );
106+ TypedQuery <Long > countQuery = session .createQuery ( cq .createCountQuery () );
99107 try {
100108 // Leads to NPE on pre-6.5 versions
101- query .getSingleResult ();
109+ countQuery .getSingleResult ();
110+ }
111+ catch (Exception e ) {
112+ fail ( e );
113+ }
114+ TypedQuery <Boolean > existsQuery = session .createQuery ( cq .createExistsQuery () );
115+ try {
116+ existsQuery .getSingleResult ();
102117 }
103118 catch (Exception e ) {
104119 fail ( e );
@@ -114,10 +129,17 @@ public void testForHHH18850(SessionFactoryScope scope) {
114129 Root <Contract > root = cq .from ( Contract .class );
115130 cq .select ( root );
116131 cq .orderBy ( cb .desc ( root .get ( "customerName" ) ) );
117- TypedQuery <Long > query = session .createQuery ( cq .createCountQuery () );
132+ TypedQuery <Long > countQuery = session .createQuery ( cq .createCountQuery () );
118133 try {
119134 // Leads to NPE on pre-6.5 versions
120- query .getSingleResult ();
135+ countQuery .getSingleResult ();
136+ }
137+ catch (Exception e ) {
138+ fail ( e );
139+ }
140+ TypedQuery <Boolean > existsQuery = session .createQuery ( cq .createExistsQuery () );
141+ try {
142+ existsQuery .getSingleResult ();
121143 }
122144 catch (Exception e ) {
123145 fail ( e );
@@ -257,8 +279,10 @@ public void testDistinctDynamicInstantiation(SessionFactoryScope scope) {
257279 )
258280 ).distinct ( true );
259281 final Long count = session .createQuery ( cq .createCountQuery () ).getSingleResult ();
282+ final Boolean exists = session .createQuery ( cq .createExistsQuery () ).getSingleResult ();
260283 final List <Tuple > resultList = session .createQuery ( cq ).getResultList ();
261284 assertEquals ( 1L , count );
285+ assertTrue ( exists );
262286 assertEquals ( resultList .size (), count .intValue () );
263287 } );
264288 }
@@ -279,8 +303,10 @@ public void testUnionQuery(SessionFactoryScope scope) {
279303
280304 final JpaCriteriaQuery <String > union = cb .union ( cq1 , cq2 );
281305 final Long count = session .createQuery ( union .createCountQuery () ).getSingleResult ();
306+ final Boolean exists = session .createQuery ( union .createExistsQuery () ).getSingleResult ();
282307 final List <String > resultList = session .createQuery ( union ).getResultList ();
283308 assertEquals ( 2L , count );
309+ assertTrue ( exists );
284310 assertEquals ( resultList .size (), count .intValue () );
285311 } );
286312 }
@@ -390,16 +416,13 @@ private <T> void verifyCount(SessionImplementor session, JpaCriteriaQuery<?> que
390416 final List <?> resultList = session .createQuery ( query ).getResultList ();
391417 final Long count = session .createQuery ( query .createCountQuery () ).getSingleResult ();
392418 assertEquals ( resultList .size (), count .intValue () );
419+ final Boolean exists = session .createQuery ( query .createExistsQuery () ).getSingleResult ();
420+ assertEquals ( !resultList .isEmpty (), exists );
393421 }
394422
395423 @ AfterEach
396424 public void dropTestData (SessionFactoryScope scope ) {
397- scope .inTransaction ( (session ) -> {
398- session .createMutationQuery ( "update Contact set alternativeContact = null" ).executeUpdate ();
399- session .createMutationQuery ( "delete Contact" ).executeUpdate ();
400- session .createMutationQuery ( "delete ChildEntity" ).executeUpdate ();
401- session .createMutationQuery ( "delete ParentEntity" ).executeUpdate ();
402- } );
425+ scope .getSessionFactory ().getSchemaManager ().truncate ();
403426 }
404427
405428 @ MappedSuperclass
0 commit comments