|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.stat.internal; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import javax.persistence.Entity; |
| 12 | +import javax.persistence.GeneratedValue; |
| 13 | +import javax.persistence.Id; |
| 14 | +import javax.persistence.NamedQuery; |
| 15 | +import javax.persistence.Table; |
| 16 | + |
| 17 | +import org.hibernate.SessionFactory; |
| 18 | +import org.hibernate.cfg.Environment; |
| 19 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 20 | +import org.hibernate.stat.Statistics; |
| 21 | + |
| 22 | +import org.hibernate.testing.TestForIssue; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; |
| 26 | +import static org.junit.Assert.assertEquals; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Gail Badner |
| 30 | + */ |
| 31 | +@TestForIssue(jiraKey = "HHH-12855") |
| 32 | +public class QueryPlanCacheStatisticsTest extends BaseEntityManagerFunctionalTestCase { |
| 33 | + |
| 34 | + private Statistics statistics; |
| 35 | + |
| 36 | + @Override |
| 37 | + public Class[] getAnnotatedClasses() { |
| 38 | + return new Class[] { |
| 39 | + Employee.class |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void addConfigOptions(Map options) { |
| 45 | + options.put( Environment.GENERATE_STATISTICS, "true" ); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected void afterEntityManagerFactoryBuilt() { |
| 50 | + SessionFactory sessionFactory = entityManagerFactory().unwrap( SessionFactory.class ); |
| 51 | + statistics = sessionFactory.getStatistics(); |
| 52 | + |
| 53 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 54 | + for ( long i = 1; i <= 5; i++ ) { |
| 55 | + if ( i % 3 == 0 ) { |
| 56 | + entityManager.flush(); |
| 57 | + } |
| 58 | + Employee employee = new Employee(); |
| 59 | + employee.setName( String.format( "Employee: %d", i ) ); |
| 60 | + entityManager.persist( employee ); |
| 61 | + } |
| 62 | + } ); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @TestForIssue( jiraKey = "HHH-14632" ) |
| 67 | + public void testCreateNativeQueryHitCount() { |
| 68 | + statistics.clear(); |
| 69 | + |
| 70 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 71 | + |
| 72 | + List<Employee> employees = entityManager.createNativeQuery( |
| 73 | + "select * from employee e", Employee.class ) |
| 74 | + .getResultList(); |
| 75 | + |
| 76 | + assertEquals( 5, employees.size() ); |
| 77 | + |
| 78 | + //First time, we get a cache miss, so the query is compiled |
| 79 | + assertEquals( 1, statistics.getQueryPlanCacheMissCount() ); |
| 80 | + //The hit count should be 0 as we don't need to go to the cache after we already compiled the query |
| 81 | + assertEquals( 0, statistics.getQueryPlanCacheHitCount() ); |
| 82 | + } ); |
| 83 | + |
| 84 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 85 | + |
| 86 | + List<Employee> employees = entityManager.createNativeQuery( |
| 87 | + "select * from employee e", Employee.class ) |
| 88 | + .getResultList(); |
| 89 | + |
| 90 | + assertEquals( 5, employees.size() ); |
| 91 | + |
| 92 | + //The miss count is still 1, as now we got the query plan from the cache |
| 93 | + assertEquals( 1, statistics.getQueryPlanCacheMissCount() ); |
| 94 | + //And the cache hit count increases. |
| 95 | + assertEquals( 1, statistics.getQueryPlanCacheHitCount() ); |
| 96 | + } ); |
| 97 | + |
| 98 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 99 | + |
| 100 | + List<Employee> employees = entityManager.createNativeQuery( |
| 101 | + "select * from employee e", Employee.class ) |
| 102 | + .getResultList(); |
| 103 | + |
| 104 | + assertEquals( 5, employees.size() ); |
| 105 | + |
| 106 | + //The miss count is still 1, as now we got the query plan from the cache |
| 107 | + assertEquals( 1, statistics.getQueryPlanCacheMissCount() ); |
| 108 | + //And the cache hit count increases. |
| 109 | + assertEquals( 2, statistics.getQueryPlanCacheHitCount() ); |
| 110 | + } ); |
| 111 | + } |
| 112 | + |
| 113 | + @Entity(name = "Employee") |
| 114 | + @Table(name = "employee") |
| 115 | + @NamedQuery( |
| 116 | + name = "find_employee_by_name", |
| 117 | + query = "select e from Employee e where e.name = :name" |
| 118 | + ) |
| 119 | + public static class Employee { |
| 120 | + |
| 121 | + @Id |
| 122 | + @GeneratedValue |
| 123 | + private Long id; |
| 124 | + |
| 125 | + private String name; |
| 126 | + |
| 127 | + public Long getId() { |
| 128 | + return id; |
| 129 | + } |
| 130 | + |
| 131 | + public void setId(Long id) { |
| 132 | + this.id = id; |
| 133 | + } |
| 134 | + |
| 135 | + public String getName() { |
| 136 | + return name; |
| 137 | + } |
| 138 | + |
| 139 | + public void setName(String name) { |
| 140 | + this.name = name; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments