88import java .util .List ;
99
1010import org .hibernate .annotations .Imported ;
11+ import org .hibernate .dialect .SybaseDialect ;
1112import org .hibernate .engine .spi .SessionImplementor ;
1213import org .hibernate .query .criteria .HibernateCriteriaBuilder ;
1314import org .hibernate .query .criteria .JpaCriteriaQuery ;
4344import jakarta .persistence .criteria .Root ;
4445
4546import static org .junit .jupiter .api .Assertions .assertEquals ;
47+ import static org .junit .jupiter .api .Assertions .assertTrue ;
4648import static org .junit .jupiter .api .Assertions .fail ;
4749
4850/**
@@ -72,10 +74,17 @@ public void testForHHH17967(SessionFactoryScope scope) {
7274 JpaCriteriaQuery <Contract > cq = cb .createQuery ( Contract .class );
7375 Root <Contract > root = cq .from ( Contract .class );
7476 cq .select ( root );
75- TypedQuery <Long > query = session .createQuery ( cq .createCountQuery () );
77+ TypedQuery <Long > countQuery = session .createQuery ( cq .createCountQuery () );
7678 try {
7779 // Leads to NPE on pre-6.5 versions
78- query .getSingleResult ();
80+ countQuery .getSingleResult ();
81+ }
82+ catch (Exception e ) {
83+ fail ( e );
84+ }
85+ TypedQuery <Boolean > existsQuery = session .createQuery ( cq .createExistsQuery () );
86+ try {
87+ existsQuery .getSingleResult ();
7988 }
8089 catch (Exception e ) {
8190 fail ( e );
@@ -95,10 +104,17 @@ public void testForHHH18850(SessionFactoryScope scope) {
95104 Root <Contract > root = cq .from ( Contract .class );
96105 cq .select ( root );
97106 cq .orderBy ( cb .asc ( root .get ( "customerName" ) ) );
98- TypedQuery <Long > query = session .createQuery ( cq .createCountQuery () );
107+ TypedQuery <Long > countQuery = session .createQuery ( cq .createCountQuery () );
99108 try {
100109 // Leads to NPE on pre-6.5 versions
101- query .getSingleResult ();
110+ countQuery .getSingleResult ();
111+ }
112+ catch (Exception e ) {
113+ fail ( e );
114+ }
115+ TypedQuery <Boolean > existsQuery = session .createQuery ( cq .createExistsQuery () );
116+ try {
117+ existsQuery .getSingleResult ();
102118 }
103119 catch (Exception e ) {
104120 fail ( e );
@@ -114,10 +130,17 @@ public void testForHHH18850(SessionFactoryScope scope) {
114130 Root <Contract > root = cq .from ( Contract .class );
115131 cq .select ( root );
116132 cq .orderBy ( cb .desc ( root .get ( "customerName" ) ) );
117- TypedQuery <Long > query = session .createQuery ( cq .createCountQuery () );
133+ TypedQuery <Long > countQuery = session .createQuery ( cq .createCountQuery () );
118134 try {
119135 // Leads to NPE on pre-6.5 versions
120- query .getSingleResult ();
136+ countQuery .getSingleResult ();
137+ }
138+ catch (Exception e ) {
139+ fail ( e );
140+ }
141+ TypedQuery <Boolean > existsQuery = session .createQuery ( cq .createExistsQuery () );
142+ try {
143+ existsQuery .getSingleResult ();
121144 }
122145 catch (Exception e ) {
123146 fail ( e );
@@ -257,8 +280,10 @@ public void testDistinctDynamicInstantiation(SessionFactoryScope scope) {
257280 )
258281 ).distinct ( true );
259282 final Long count = session .createQuery ( cq .createCountQuery () ).getSingleResult ();
283+ final Boolean exists = session .createQuery ( cq .createExistsQuery () ).getSingleResult ();
260284 final List <Tuple > resultList = session .createQuery ( cq ).getResultList ();
261285 assertEquals ( 1L , count );
286+ assertTrue ( exists );
262287 assertEquals ( resultList .size (), count .intValue () );
263288 } );
264289 }
@@ -278,6 +303,10 @@ public void testUnionQuery(SessionFactoryScope scope) {
278303 cq2 .select ( root2 .get ( "name" ).get ( "first" ) ).where ( cb .equal ( root2 .get ( "id" ), 2 ) );
279304
280305 final JpaCriteriaQuery <String > union = cb .union ( cq1 , cq2 );
306+ if ( !(scope .getSessionFactory ().getJdbcServices ().getDialect () instanceof SybaseDialect ) ) {
307+ final Boolean exists = session .createQuery ( union .createExistsQuery () ).getSingleResult ();
308+ assertTrue ( exists );
309+ }
281310 final Long count = session .createQuery ( union .createCountQuery () ).getSingleResult ();
282311 final List <String > resultList = session .createQuery ( union ).getResultList ();
283312 assertEquals ( 2L , count );
@@ -390,16 +419,13 @@ private <T> void verifyCount(SessionImplementor session, JpaCriteriaQuery<?> que
390419 final List <?> resultList = session .createQuery ( query ).getResultList ();
391420 final Long count = session .createQuery ( query .createCountQuery () ).getSingleResult ();
392421 assertEquals ( resultList .size (), count .intValue () );
422+ final Boolean exists = session .createQuery ( query .createExistsQuery () ).getSingleResult ();
423+ assertEquals ( !resultList .isEmpty (), exists );
393424 }
394425
395426 @ AfterEach
396427 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- } );
428+ scope .getSessionFactory ().getSchemaManager ().truncate ();
403429 }
404430
405431 @ MappedSuperclass
0 commit comments