44 */
55package org .hibernate .orm .test .stat .internal ;
66
7- import java .util .List ;
87import jakarta .persistence .Entity ;
98import jakarta .persistence .GeneratedValue ;
109import jakarta .persistence .Id ;
1312import jakarta .persistence .Table ;
1413import jakarta .persistence .Tuple ;
1514import jakarta .persistence .TypedQuery ;
16-
1715import org .hibernate .cfg .AvailableSettings ;
18- import org .hibernate .cfg .Environment ;
19- import org .hibernate .engine .spi .SessionFactoryImplementor ;
2016import org .hibernate .jpa .AvailableHints ;
2117import org .hibernate .query .Query ;
2218import org .hibernate .query .criteria .HibernateCriteriaBuilder ;
2319import org .hibernate .query .criteria .JpaCriteriaQuery ;
2420import org .hibernate .stat .QueryStatistics ;
25- import org .hibernate .stat .Statistics ;
26-
27- import org .hibernate .testing .orm .junit .JiraKey ;
21+ import org .hibernate .stat .spi .StatisticsImplementor ;
2822import org .hibernate .testing .orm .junit .DomainModel ;
29- import org .hibernate .testing .orm .junit .ServiceRegistry ;
23+ import org .hibernate .testing .orm .junit .JiraKey ;
3024import org .hibernate .testing .orm .junit .SessionFactory ;
3125import org .hibernate .testing .orm .junit .SessionFactoryScope ;
32- import org .hibernate . testing . orm . junit . Setting ;
33- import org .junit .jupiter .api .BeforeAll ;
26+ import org .junit . jupiter . api . AfterEach ;
27+ import org .junit .jupiter .api .Assertions ;
3428import org .junit .jupiter .api .BeforeEach ;
3529import org .junit .jupiter .api .Test ;
3630
37- import static org .junit .Assert .assertEquals ;
38- import static org .junit .Assert .assertNotNull ;
39- import static org .junit .Assert .assertTrue ;
31+ import java .util .List ;
32+
33+ import static org .junit .jupiter .api .Assertions .assertEquals ;
34+ import static org .junit .jupiter .api .Assertions .assertTrue ;
35+
4036
4137/**
4238 * @author Gail Badner
4339 */
44- @ DomainModel (annotatedClasses = {
45- QueryPlanCacheStatisticsTest .Employee .class
46- })
47- @ ServiceRegistry (settings = {
48- @ Setting ( name = Environment .GENERATE_STATISTICS , value = "true" )
49- })
50- @ SessionFactory
40+ @ SuppressWarnings ("JUnitMalformedDeclaration" )
41+ @ DomainModel (annotatedClasses = QueryPlanCacheStatisticsTest .Employee .class )
42+ @ SessionFactory (generateStatistics = true )
5143@ JiraKey ("HHH-12855" )
5244public class QueryPlanCacheStatisticsTest {
53-
54- private Statistics statistics ;
55-
56- @ BeforeAll
57- protected void afterEntityManagerFactoryBuilt (SessionFactoryScope scope ) {
58- scope .inTransaction ( entityManager -> {
45+ @ BeforeEach
46+ public void setUp (SessionFactoryScope factoryScope ) {
47+ factoryScope .inTransaction ( entityManager -> {
5948 for ( long i = 1 ; i <= 5 ; i ++ ) {
6049 Employee employee = new Employee ();
6150 employee .setName ( String .format ( "Employee: %d" , i ) );
6251 entityManager .persist ( employee );
6352 }
6453 } );
54+
55+ factoryScope .getSessionFactory ().getStatistics ().clear ();
56+ factoryScope .getSessionFactory ().getQueryEngine ().getInterpretationCache ().close ();
6557 }
6658
67- @ BeforeEach
68- protected void cleanup (SessionFactoryScope scope ) {
69- final SessionFactoryImplementor sessionFactory = scope .getSessionFactory ();
70- statistics = sessionFactory .getStatistics ();
71- statistics .clear ();
72- sessionFactory .getQueryEngine ().getInterpretationCache ().close ();
59+ @ AfterEach
60+ void tearDown (SessionFactoryScope factoryScope ) {
61+ factoryScope .dropData ();
7362 }
7463
7564 @ Test
7665 public void test (SessionFactoryScope scope ) {
66+ final StatisticsImplementor statistics = scope .getSessionFactory ().getStatistics ();
67+
7768 assertEquals ( 0 , statistics .getQueryPlanCacheHitCount () );
7869 assertEquals ( 0 , statistics .getQueryPlanCacheMissCount () );
7970
@@ -84,53 +75,48 @@ public void test(SessionFactoryScope scope) {
8475
8576 assertEquals ( 0 , statistics .getQueryPlanCacheHitCount () );
8677 assertEquals ( 1 , statistics .getQueryPlanCacheMissCount () );
87-
88- assertQueryStatistics ( FIRST_QUERY , 0 );
78+ assertQueryStatistics ( FIRST_QUERY , 0 , statistics );
8979
9080 entityManager .createQuery ( FIRST_QUERY );
9181
9282 assertEquals ( 1 , statistics .getQueryPlanCacheHitCount () );
9383 assertEquals ( 1 , statistics .getQueryPlanCacheMissCount () );
94-
95- assertQueryStatistics ( FIRST_QUERY , 1 );
84+ assertQueryStatistics ( FIRST_QUERY , 1 , statistics );
9685
9786 entityManager .createQuery ( FIRST_QUERY );
9887
9988 assertEquals ( 2 , statistics .getQueryPlanCacheHitCount () );
10089 assertEquals ( 1 , statistics .getQueryPlanCacheMissCount () );
101-
102- assertQueryStatistics ( FIRST_QUERY , 2 );
90+ assertQueryStatistics ( FIRST_QUERY , 2 , statistics );
10391
10492 final String SECOND_QUERY = "select count(e) from Employee e" ;
10593
10694 entityManager .createQuery ( SECOND_QUERY );
10795
10896 assertEquals ( 2 , statistics .getQueryPlanCacheHitCount () );
10997 assertEquals ( 2 , statistics .getQueryPlanCacheMissCount () );
110-
111- assertQueryStatistics ( SECOND_QUERY , 0 );
98+ assertQueryStatistics ( SECOND_QUERY , 0 , statistics );
11299
113100 entityManager .createQuery ( SECOND_QUERY );
114101
115102 assertEquals ( 3 , statistics .getQueryPlanCacheHitCount () );
116103 assertEquals ( 2 , statistics .getQueryPlanCacheMissCount () );
117-
118- assertQueryStatistics ( SECOND_QUERY , 1 );
104+ assertQueryStatistics ( SECOND_QUERY , 1 , statistics );
119105
120106 entityManager .createQuery ( SECOND_QUERY );
121107
122108 assertEquals ( 4 , statistics .getQueryPlanCacheHitCount () );
123109 assertEquals ( 2 , statistics .getQueryPlanCacheMissCount () );
124-
125- assertQueryStatistics ( SECOND_QUERY , 2 );
110+ assertQueryStatistics ( SECOND_QUERY , 2 , statistics );
126111 } );
127112 }
128113
129114 @ Test
130115 @ JiraKey ("HHH-13077" )
131116 public void testCreateQueryHitCount (SessionFactoryScope scope ) {
132- scope . inTransaction ( entityManager -> {
117+ final StatisticsImplementor statistics = scope . getSessionFactory (). getStatistics ();
133118
119+ scope .inTransaction ( entityManager -> {
134120 Query <Employee > query = entityManager .createQuery (
135121 "select e from Employee e" , Employee .class );
136122
@@ -179,10 +165,9 @@ public void testCreateQueryHitCount(SessionFactoryScope scope) {
179165 @ Test
180166 @ JiraKey ("HHH-14632" )
181167 public void testCreateNativeQueryHitCount (SessionFactoryScope scope ) {
182- statistics . clear ();
168+ var statistics = scope . getSessionFactory (). getStatistics ();
183169
184170 scope .inTransaction ( entityManager -> {
185-
186171 List <Employee > employees = entityManager .createNativeQuery (
187172 "select * from employee e" , Employee .class )
188173 .getResultList ();
@@ -196,7 +181,6 @@ public void testCreateNativeQueryHitCount(SessionFactoryScope scope) {
196181 } );
197182
198183 scope .inTransaction ( entityManager -> {
199-
200184 List <Employee > employees = entityManager .createNativeQuery (
201185 "select * from employee e" , Employee .class )
202186 .getResultList ();
@@ -210,7 +194,6 @@ public void testCreateNativeQueryHitCount(SessionFactoryScope scope) {
210194 } );
211195
212196 scope .inTransaction ( entityManager -> {
213-
214197 List <Employee > employees = entityManager .createNativeQuery (
215198 "select * from employee e" , Employee .class )
216199 .getResultList ();
@@ -229,9 +212,11 @@ public void testCreateNativeQueryHitCount(SessionFactoryScope scope) {
229212 public void testCreateNamedQueryHitCount (SessionFactoryScope scope ) {
230213 // Compile the named queries
231214 scope .getSessionFactory ().getQueryEngine ().getNamedObjectRepository ().checkNamedQueries ( scope .getSessionFactory ().getQueryEngine () );
215+
216+ var statistics = scope .getSessionFactory ().getStatistics ();
232217 statistics .clear ();
233- scope .inTransaction ( entityManager -> {
234218
219+ scope .inTransaction ( entityManager -> {
235220 Query <Employee > query = entityManager .createNamedQuery (
236221 "find_employee_by_name" , Employee .class )
237222 .setParameter ( "name" , "Employee: 1" );
@@ -249,7 +234,6 @@ public void testCreateNamedQueryHitCount(SessionFactoryScope scope) {
249234 } );
250235
251236 scope .inTransaction ( entityManager -> {
252-
253237 Employee employees = entityManager .createNamedQuery (
254238 "find_employee_by_name" , Employee .class )
255239 .setParameter ( "name" , "Employee: 1" )
@@ -266,8 +250,9 @@ public void testCreateNamedQueryHitCount(SessionFactoryScope scope) {
266250 @ Test
267251 @ JiraKey ("HHH-13077" )
268252 public void testCreateQueryTupleHitCount (SessionFactoryScope scope ) {
269- scope . inTransaction ( entityManager -> {
253+ var statistics = scope . getSessionFactory (). getStatistics ();
270254
255+ scope .inTransaction ( entityManager -> {
271256 Query <Tuple > query = entityManager .createQuery (
272257 "select e.id, e.name from Employee e" , Tuple .class );
273258
@@ -286,7 +271,6 @@ public void testCreateQueryTupleHitCount(SessionFactoryScope scope) {
286271 } );
287272
288273 scope .inTransaction ( entityManager -> {
289-
290274 List <Tuple > employees = entityManager .createQuery (
291275 "select e.id, e.name from Employee e" , Tuple .class )
292276 .getResultList ();
@@ -300,7 +284,6 @@ public void testCreateQueryTupleHitCount(SessionFactoryScope scope) {
300284 } );
301285
302286 scope .inTransaction ( entityManager -> {
303-
304287 List <Tuple > employees = entityManager .createQuery (
305288 "select e.id, e.name from Employee e" , Tuple .class )
306289 .getResultList ();
@@ -317,6 +300,8 @@ public void testCreateQueryTupleHitCount(SessionFactoryScope scope) {
317300 @ Test
318301 @ JiraKey ("HHH-13077" )
319302 public void testLockModeHitCount (SessionFactoryScope scope ) {
303+ var statistics = scope .getSessionFactory ().getStatistics ();
304+
320305 scope .inTransaction ( entityManager -> {
321306 TypedQuery <Employee > typedQuery = entityManager .createQuery ( "select e from Employee e" , Employee .class );
322307
@@ -338,7 +323,7 @@ public void testLockModeHitCount(SessionFactoryScope scope) {
338323 //The hit count should still be 0 as setLockMode() shouldn't trigger a cache hit
339324 assertEquals ( 0 , statistics .getQueryPlanCacheHitCount () );
340325
341- assertNotNull ( typedQuery .getLockMode () );
326+ Assertions . assertNotNull ( typedQuery .getLockMode () );
342327
343328 //The hit count should still be 0 as getLockMode() shouldn't trigger a cache hit
344329 assertEquals ( 0 , statistics .getQueryPlanCacheHitCount () );
@@ -348,6 +333,8 @@ public void testLockModeHitCount(SessionFactoryScope scope) {
348333 @ Test
349334 @ JiraKey ("HHH-16782" )
350335 public void testCriteriaQuery (SessionFactoryScope scope ) {
336+ var statistics = scope .getSessionFactory ().getStatistics ();
337+
351338 scope .inTransaction ( entityManager -> {
352339 HibernateCriteriaBuilder cb = entityManager .getCriteriaBuilder ();
353340 JpaCriteriaQuery <Employee > cq = cb .createQuery ( Employee .class );
@@ -380,6 +367,8 @@ public void testCriteriaQuery(SessionFactoryScope scope) {
380367 @ Test
381368 @ JiraKey ("HHH-16782" )
382369 public void testCriteriaQueryCache (SessionFactoryScope scope ) {
370+ var statistics = scope .getSessionFactory ().getStatistics ();
371+
383372 scope .inTransaction ( entityManager -> {
384373 HibernateCriteriaBuilder cb = entityManager .getCriteriaBuilder ();
385374 JpaCriteriaQuery <Employee > cq = cb .createQuery ( Employee .class );
@@ -412,6 +401,8 @@ public void testCriteriaQueryCache(SessionFactoryScope scope) {
412401 @ Test
413402 @ JiraKey ("HHH-16782" )
414403 public void testCriteriaQueryNoCopyTree (SessionFactoryScope scope ) {
404+ var statistics = scope .getSessionFactory ().getStatistics ();
405+
415406 scope .inTransaction ( entityManager -> {
416407 HibernateCriteriaBuilder cb = entityManager .getCriteriaBuilder ();
417408 JpaCriteriaQuery <Employee > cq = cb .createQuery ( Employee .class );
@@ -444,6 +435,8 @@ public void testCriteriaQueryNoCopyTree(SessionFactoryScope scope) {
444435 @ Test
445436 @ JiraKey ("HHH-16782" )
446437 public void testDisableQueryPlanCache (SessionFactoryScope scope ) {
438+ var statistics = scope .getSessionFactory ().getStatistics ();
439+
447440 scope .inTransaction ( entityManager -> {
448441 TypedQuery <Employee > typedQuery = entityManager .createQuery ( "select e from Employee e" , Employee .class );
449442 typedQuery .setHint ( AvailableHints .HINT_QUERY_PLAN_CACHEABLE , false );
@@ -471,7 +464,7 @@ public void testDisableQueryPlanCache(SessionFactoryScope scope) {
471464 } );
472465 }
473466
474- private void assertQueryStatistics (String hql , int hitCount ) {
467+ private void assertQueryStatistics (String hql , int hitCount , StatisticsImplementor statistics ) {
475468 QueryStatistics queryStatistics = statistics .getQueryStatistics ( hql );
476469
477470 assertEquals ( hitCount , queryStatistics .getPlanCacheHitCount () );
0 commit comments