HHH-15666 Add test for issue
This commit is contained in:
parent
ca79c91c56
commit
865f28a5d5
|
@ -0,0 +1,322 @@
|
||||||
|
/*
|
||||||
|
* 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.orm.test.jpa.query;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.hibernate.annotations.Immutable;
|
||||||
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
|
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||||
|
import org.hibernate.engine.spi.SessionImplementor;
|
||||||
|
import org.hibernate.stat.Statistics;
|
||||||
|
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
||||||
|
import org.hibernate.testing.orm.junit.Jpa;
|
||||||
|
import org.hibernate.testing.orm.junit.Setting;
|
||||||
|
import org.hibernate.testing.orm.junit.SettingProvider;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.SharedCacheMode;
|
||||||
|
import jakarta.persistence.TypedQuery;
|
||||||
|
|
||||||
|
import static org.hibernate.jpa.HibernateHints.HINT_CACHEABLE;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
@TestForIssue(jiraKey = "HHH-15086")
|
||||||
|
@Jpa(
|
||||||
|
annotatedClasses = CachedQueryDirectReferenceTest.ImmutableEmployee.class,
|
||||||
|
generateStatistics = true,
|
||||||
|
properties = {
|
||||||
|
@Setting(name = AvailableSettings.USE_QUERY_CACHE, value = "true"),
|
||||||
|
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"),
|
||||||
|
@Setting(name = AvailableSettings.USE_DIRECT_REFERENCE_CACHE_ENTRIES, value = "true"),
|
||||||
|
},
|
||||||
|
settingProviders = @SettingProvider(settingName = AvailableSettings.JAKARTA_SHARED_CACHE_MODE, provider = CachedQueryDirectReferenceTest.SharedCacheModeProvider.class)
|
||||||
|
)
|
||||||
|
public class CachedQueryDirectReferenceTest {
|
||||||
|
|
||||||
|
public final static String HQL = "select e from ImmutableEmployee e";
|
||||||
|
|
||||||
|
public static class SharedCacheModeProvider implements SettingProvider.Provider<SharedCacheMode> {
|
||||||
|
@Override
|
||||||
|
public SharedCacheMode getSetting() {
|
||||||
|
return SharedCacheMode.ALL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp(EntityManagerFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
em -> {
|
||||||
|
for ( int i = 0; i < 10; i++ ) {
|
||||||
|
em.persist( new ImmutableEmployee( "John" + i, 20d + i ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
Statistics stats = getStatistics( scope );
|
||||||
|
|
||||||
|
assertEquals( 0, stats.getQueryCacheHitCount() );
|
||||||
|
assertEquals( 0, stats.getQueryCacheMissCount() );
|
||||||
|
assertEquals( 0, stats.getQueryCachePutCount() );
|
||||||
|
assertEquals( 0, stats.getSecondLevelCacheHitCount() );
|
||||||
|
assertEquals( 0, stats.getSecondLevelCacheMissCount() );
|
||||||
|
assertEquals( 10, stats.getSecondLevelCachePutCount() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void tearDown(EntityManagerFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
em ->
|
||||||
|
em.createQuery( "delete from ImmutableEmployee" ).executeUpdate()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCacheableQuery(EntityManagerFactoryScope scope) {
|
||||||
|
Statistics stats = getStatistics( scope );
|
||||||
|
stats.clear();
|
||||||
|
|
||||||
|
// First time the query is executed, query and results are cached.
|
||||||
|
scope.inTransaction(
|
||||||
|
em -> {
|
||||||
|
List<ImmutableEmployee> employees = getEmployees( em );
|
||||||
|
|
||||||
|
assertThatAnSQLQueryHasBeenExecuted( stats );
|
||||||
|
|
||||||
|
assertEquals( 0, stats.getQueryCacheHitCount() );
|
||||||
|
assertEquals( 1, stats.getQueryCacheMissCount() );
|
||||||
|
assertEquals( 1, stats.getQueryCachePutCount() );
|
||||||
|
|
||||||
|
// because the entity is immutable and USE_DIRECT_REFERENCE_CACHE_ENTRIES has been set to true, the
|
||||||
|
// second level cache is hit for a cached references, if it does not contain any
|
||||||
|
// reference then the query cache values are used to initialize the entity
|
||||||
|
assertEquals( 10, stats.getSecondLevelCacheHitCount() );
|
||||||
|
assertEquals( 0, stats.getSecondLevelCacheMissCount() );
|
||||||
|
assertEquals( 0, stats.getSecondLevelCachePutCount() );
|
||||||
|
|
||||||
|
// because the entity is obtained from reference stored in the second level cache, no setter methods has been called
|
||||||
|
assertThatNoSetterMethodHasBeenCalled( employees );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
stats.clear();
|
||||||
|
|
||||||
|
// Second time the query is executed, list of entities are read from second level cache
|
||||||
|
|
||||||
|
scope.inTransaction(
|
||||||
|
em -> {
|
||||||
|
List<ImmutableEmployee> employees = getEmployees( em );
|
||||||
|
|
||||||
|
assertThatNoSQLQueryHasBeenExecuted( stats );
|
||||||
|
|
||||||
|
assertEquals( 1, stats.getQueryCacheHitCount() );
|
||||||
|
assertEquals( 0, stats.getQueryCacheMissCount() );
|
||||||
|
assertEquals( 0, stats.getQueryCachePutCount() );
|
||||||
|
|
||||||
|
assertEquals( 10, stats.getSecondLevelCacheHitCount() );
|
||||||
|
assertEquals( 0, stats.getSecondLevelCacheMissCount() );
|
||||||
|
assertEquals( 0, stats.getSecondLevelCachePutCount() );
|
||||||
|
|
||||||
|
// because the entity is obtained from reference stored in the second level cache, no setter methods has been called
|
||||||
|
assertThatNoSetterMethodHasBeenCalled( employees );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// NOTE: JPACache.evictAll() only evicts entity regions;
|
||||||
|
// it does not evict the collection regions or query cache region
|
||||||
|
scope.getEntityManagerFactory().getCache().evictAll();
|
||||||
|
|
||||||
|
stats.clear();
|
||||||
|
|
||||||
|
scope.inTransaction(
|
||||||
|
em -> {
|
||||||
|
List<ImmutableEmployee> employees = getEmployees( em );
|
||||||
|
|
||||||
|
// query is still found in the cache
|
||||||
|
assertThatNoSQLQueryHasBeenExecuted( stats );
|
||||||
|
|
||||||
|
assertEquals( 1, stats.getQueryCacheHitCount() );
|
||||||
|
assertEquals( 0, stats.getQueryCacheMissCount() );
|
||||||
|
assertEquals( 0, stats.getQueryCachePutCount() );
|
||||||
|
|
||||||
|
assertEquals( 0, stats.getSecondLevelCacheHitCount() );
|
||||||
|
assertEquals( 10, stats.getSecondLevelCacheMissCount() );
|
||||||
|
assertEquals( 0, stats.getSecondLevelCachePutCount() );
|
||||||
|
|
||||||
|
// the entity is
|
||||||
|
assertThatSetterMehotdsHaveBeenCalled( employees );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
stats.clear();
|
||||||
|
|
||||||
|
// this time call clear the entity regions and the query cache region
|
||||||
|
scope.inTransaction(
|
||||||
|
em -> {
|
||||||
|
em.getEntityManagerFactory().getCache().evictAll();
|
||||||
|
em.unwrap( SessionImplementor.class )
|
||||||
|
.getFactory()
|
||||||
|
.getCache()
|
||||||
|
.evictQueryRegions();
|
||||||
|
|
||||||
|
List<ImmutableEmployee> employees = getEmployees( em );
|
||||||
|
|
||||||
|
|
||||||
|
// query is no longer found in the cache, so the query is executed
|
||||||
|
assertThatAnSQLQueryHasBeenExecuted( stats );
|
||||||
|
|
||||||
|
assertEquals( 0, stats.getQueryCacheHitCount() );
|
||||||
|
assertEquals( 1, stats.getQueryCacheMissCount() );
|
||||||
|
assertEquals( 1, stats.getQueryCachePutCount() );
|
||||||
|
|
||||||
|
assertEquals( 0, stats.getSecondLevelCacheHitCount() );
|
||||||
|
assertEquals( 10, stats.getSecondLevelCacheMissCount() );
|
||||||
|
assertEquals( 10, stats.getSecondLevelCachePutCount() );
|
||||||
|
|
||||||
|
assertThatSetterMehotdsHaveBeenCalled( employees );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertThatSetterMehotdsHaveBeenCalled(List<ImmutableEmployee> employees) {
|
||||||
|
assertEquals( 1, employees.get( 0 ).callToSetter );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertThatNoSetterMethodHasBeenCalled(List<ImmutableEmployee> employees) {
|
||||||
|
assertEquals( 0, employees.get( 0 ).callToSetter );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertThatAnSQLQueryHasBeenExecuted(Statistics stats) {
|
||||||
|
assertEquals( 1, stats.getQueryStatistics( HQL ).getExecutionCount() );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertThatNoSQLQueryHasBeenExecuted(Statistics stats) {
|
||||||
|
assertEquals( 0, stats.getQueryStatistics( HQL ).getExecutionCount() );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Statistics getStatistics(EntityManagerFactoryScope scope) {
|
||||||
|
return ( (SessionFactoryImplementor) scope.getEntityManagerFactory() ).getStatistics();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<ImmutableEmployee> getEmployees(EntityManager em) {
|
||||||
|
TypedQuery<ImmutableEmployee> query = em.createQuery(
|
||||||
|
HQL,
|
||||||
|
ImmutableEmployee.class
|
||||||
|
)
|
||||||
|
.setHint( HINT_CACHEABLE, true );
|
||||||
|
List<ImmutableEmployee> employees = query.getResultList();
|
||||||
|
assertEquals( 10, employees.size() );
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void save(EntityManagerFactoryScope scope, int cardinality) {
|
||||||
|
scope.inTransaction(
|
||||||
|
em -> {
|
||||||
|
for ( int i = 0; i < cardinality; i++ ) {
|
||||||
|
em.persist( new ImmutableEmployee( "John" + i, 20d + i ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity(name = "ImmutableEmployee")
|
||||||
|
@Immutable
|
||||||
|
public static class ImmutableEmployee implements Serializable {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private Double salary;
|
||||||
|
|
||||||
|
private int callToSetter;
|
||||||
|
|
||||||
|
public ImmutableEmployee() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ImmutableEmployee(String name, Double salary) {
|
||||||
|
this.name = name;
|
||||||
|
this.salary = salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if ( this == o ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ( !( o instanceof org.hibernate.orm.test.jpa.query.Employee ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImmutableEmployee employee = (ImmutableEmployee) o;
|
||||||
|
|
||||||
|
if ( id != null ? !id.equals( employee.id ) : employee.id != null ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ( name != null ? !name.equals( employee.name ) : employee.name != null ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ( salary != null ? !salary.equals( employee.salary ) : employee.salary != null ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
callToSetter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getSalary() {
|
||||||
|
return salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSalary(Double salary) {
|
||||||
|
this.salary = salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = id != null ? id.hashCode() : 0;
|
||||||
|
result = 31 * result + ( name != null ? name.hashCode() : 0 );
|
||||||
|
result = 31 * result + ( salary != null ? salary.hashCode() : 0 );
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Employee(id = " + id + ", name = " + name + ", salary = " + salary + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,7 +7,6 @@
|
||||||
package org.hibernate.orm.test.jpa.query;
|
package org.hibernate.orm.test.jpa.query;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||||
|
@ -19,8 +18,11 @@ import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
||||||
import org.hibernate.testing.orm.junit.Jpa;
|
import org.hibernate.testing.orm.junit.Jpa;
|
||||||
import org.hibernate.testing.orm.junit.Setting;
|
import org.hibernate.testing.orm.junit.Setting;
|
||||||
import org.hibernate.testing.orm.junit.SettingProvider;
|
import org.hibernate.testing.orm.junit.SettingProvider;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
import jakarta.persistence.SharedCacheMode;
|
import jakarta.persistence.SharedCacheMode;
|
||||||
import jakarta.persistence.TypedQuery;
|
import jakarta.persistence.TypedQuery;
|
||||||
|
|
||||||
|
@ -42,9 +44,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
)
|
)
|
||||||
public class CachedQueryTest {
|
public class CachedQueryTest {
|
||||||
|
|
||||||
protected void addConfigOptions(Map options) {
|
public final static String HQL = "select e from Employee e";
|
||||||
options.put( AvailableSettings.JPA_SHARED_CACHE_MODE, SharedCacheMode.ALL );
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class SharedCacheModeProvider implements SettingProvider.Provider<SharedCacheMode> {
|
public static class SharedCacheModeProvider implements SettingProvider.Provider<SharedCacheMode> {
|
||||||
@Override
|
@Override
|
||||||
|
@ -53,20 +53,16 @@ public class CachedQueryTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
@Test
|
public void setUp(EntityManagerFactoryScope scope) {
|
||||||
public void testCacheableQuery(EntityManagerFactoryScope scope) {
|
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
em -> {
|
em -> {
|
||||||
for ( int i = 0; i < 10; i++ ) {
|
for ( int i = 0; i < 10; i++ ) {
|
||||||
Employee employee = new Employee( "John" + i, 20d + i );
|
em.persist( new Employee( "John" + i, 20d + i ) );
|
||||||
em.persist( employee );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
Statistics stats = getStatistics( scope );
|
||||||
SessionFactoryImplementor hemf = (SessionFactoryImplementor) scope.getEntityManagerFactory();
|
|
||||||
Statistics stats = hemf.getStatistics();
|
|
||||||
|
|
||||||
assertEquals( 0, stats.getQueryCacheHitCount() );
|
assertEquals( 0, stats.getQueryCacheHitCount() );
|
||||||
assertEquals( 0, stats.getQueryCacheMissCount() );
|
assertEquals( 0, stats.getQueryCacheMissCount() );
|
||||||
|
@ -74,17 +70,29 @@ public class CachedQueryTest {
|
||||||
assertEquals( 0, stats.getSecondLevelCacheHitCount() );
|
assertEquals( 0, stats.getSecondLevelCacheHitCount() );
|
||||||
assertEquals( 0, stats.getSecondLevelCacheMissCount() );
|
assertEquals( 0, stats.getSecondLevelCacheMissCount() );
|
||||||
assertEquals( 10, stats.getSecondLevelCachePutCount() );
|
assertEquals( 10, stats.getSecondLevelCachePutCount() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void tearDown(EntityManagerFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
em ->
|
||||||
|
em.createQuery( "delete from Employee" ).executeUpdate()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCacheableQuery(EntityManagerFactoryScope scope) {
|
||||||
|
|
||||||
|
Statistics stats = getStatistics( scope );
|
||||||
stats.clear();
|
stats.clear();
|
||||||
|
|
||||||
// First time the query is executed, query and results are cached.
|
// First time the query is executed, query and results are cached.
|
||||||
|
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
em -> {
|
em -> {
|
||||||
TypedQuery<Employee> query = em.createQuery( "select e from Employee e", Employee.class )
|
List<Employee> employees = getEmployees( em );
|
||||||
.setHint( HINT_CACHEABLE, true );
|
|
||||||
List<Employee> employees = query.getResultList();
|
assertThatAnSQLQueryHasBeenExecuted( stats );
|
||||||
assertEquals( 10, employees.size() );
|
|
||||||
assertEquals( 0, stats.getQueryCacheHitCount() );
|
assertEquals( 0, stats.getQueryCacheHitCount() );
|
||||||
assertEquals( 1, stats.getQueryCacheMissCount() );
|
assertEquals( 1, stats.getQueryCacheMissCount() );
|
||||||
assertEquals( 1, stats.getQueryCachePutCount() );
|
assertEquals( 1, stats.getQueryCachePutCount() );
|
||||||
|
@ -101,10 +109,10 @@ public class CachedQueryTest {
|
||||||
|
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
em -> {
|
em -> {
|
||||||
TypedQuery<Employee> query = em.createQuery( "select e from Employee e", Employee.class )
|
List<Employee> employees = getEmployees( em );
|
||||||
.setHint( HINT_CACHEABLE, true );
|
|
||||||
List<Employee> employees = query.getResultList();
|
assertThatNoSQLQueryHasBeenExecuted( stats );
|
||||||
assertEquals( 10, employees.size() );
|
|
||||||
assertEquals( 1, stats.getQueryCacheHitCount() );
|
assertEquals( 1, stats.getQueryCacheHitCount() );
|
||||||
assertEquals( 0, stats.getQueryCacheMissCount() );
|
assertEquals( 0, stats.getQueryCacheMissCount() );
|
||||||
assertEquals( 0, stats.getQueryCachePutCount() );
|
assertEquals( 0, stats.getQueryCachePutCount() );
|
||||||
|
@ -115,7 +123,6 @@ public class CachedQueryTest {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
// NOTE: JPACache.evictAll() only evicts entity regions;
|
// NOTE: JPACache.evictAll() only evicts entity regions;
|
||||||
// it does not evict the collection regions or query cache region
|
// it does not evict the collection regions or query cache region
|
||||||
scope.getEntityManagerFactory().getCache().evictAll();
|
scope.getEntityManagerFactory().getCache().evictAll();
|
||||||
|
@ -124,11 +131,11 @@ public class CachedQueryTest {
|
||||||
|
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
em -> {
|
em -> {
|
||||||
TypedQuery<Employee> query = em.createQuery( "select e from Employee e", Employee.class )
|
List<Employee> employees = getEmployees( em );
|
||||||
.setHint( HINT_CACHEABLE, true );
|
|
||||||
List<Employee> employees = query.getResultList();
|
|
||||||
assertEquals( 10, employees.size() );
|
|
||||||
// query is still found in the cache
|
// query is still found in the cache
|
||||||
|
assertThatNoSQLQueryHasBeenExecuted( stats );
|
||||||
|
|
||||||
assertEquals( 1, stats.getQueryCacheHitCount() );
|
assertEquals( 1, stats.getQueryCacheHitCount() );
|
||||||
assertEquals( 0, stats.getQueryCacheMissCount() );
|
assertEquals( 0, stats.getQueryCacheMissCount() );
|
||||||
assertEquals( 0, stats.getQueryCachePutCount() );
|
assertEquals( 0, stats.getQueryCachePutCount() );
|
||||||
|
@ -143,7 +150,7 @@ public class CachedQueryTest {
|
||||||
stats.clear();
|
stats.clear();
|
||||||
|
|
||||||
// this time call clear the entity regions and the query cache region
|
// this time call clear the entity regions and the query cache region
|
||||||
scope.inEntityManager(
|
scope.inTransaction(
|
||||||
em -> {
|
em -> {
|
||||||
em.getEntityManagerFactory().getCache().evictAll();
|
em.getEntityManagerFactory().getCache().evictAll();
|
||||||
em.unwrap( SessionImplementor.class )
|
em.unwrap( SessionImplementor.class )
|
||||||
|
@ -151,13 +158,11 @@ public class CachedQueryTest {
|
||||||
.getCache()
|
.getCache()
|
||||||
.evictQueryRegions();
|
.evictQueryRegions();
|
||||||
|
|
||||||
em.getTransaction().begin();
|
List<Employee> employees = getEmployees( em );
|
||||||
try {
|
|
||||||
TypedQuery<Employee> query = em.createQuery( "select e from Employee e", Employee.class )
|
|
||||||
.setHint( HINT_CACHEABLE, true );
|
|
||||||
List<Employee> employees = query.getResultList();
|
|
||||||
assertEquals( 10, employees.size() );
|
|
||||||
// query is no longer found in the cache
|
// query is no longer found in the cache
|
||||||
|
assertThatAnSQLQueryHasBeenExecuted( stats );
|
||||||
|
|
||||||
assertEquals( 0, stats.getQueryCacheHitCount() );
|
assertEquals( 0, stats.getQueryCacheHitCount() );
|
||||||
assertEquals( 1, stats.getQueryCacheMissCount() );
|
assertEquals( 1, stats.getQueryCacheMissCount() );
|
||||||
assertEquals( 1, stats.getQueryCachePutCount() );
|
assertEquals( 1, stats.getQueryCachePutCount() );
|
||||||
|
@ -165,18 +170,32 @@ public class CachedQueryTest {
|
||||||
assertEquals( 0, stats.getSecondLevelCacheHitCount() );
|
assertEquals( 0, stats.getSecondLevelCacheHitCount() );
|
||||||
assertEquals( 0, stats.getSecondLevelCacheMissCount() );
|
assertEquals( 0, stats.getSecondLevelCacheMissCount() );
|
||||||
assertEquals( 10, stats.getSecondLevelCachePutCount() );
|
assertEquals( 10, stats.getSecondLevelCachePutCount() );
|
||||||
|
|
||||||
em.createQuery( "delete from Employee" ).executeUpdate();
|
|
||||||
em.getTransaction().commit();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if ( em.getTransaction().isActive() ) {
|
|
||||||
em.getTransaction().rollback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Statistics getStatistics(EntityManagerFactoryScope scope) {
|
||||||
|
return ( (SessionFactoryImplementor) scope.getEntityManagerFactory() ).getStatistics();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<Employee> getEmployees(EntityManager em) {
|
||||||
|
TypedQuery<Employee> query = em.createQuery(
|
||||||
|
HQL,
|
||||||
|
Employee.class
|
||||||
|
)
|
||||||
|
.setHint( HINT_CACHEABLE, true );
|
||||||
|
List<Employee> employees = query.getResultList();
|
||||||
|
assertEquals( 10, employees.size() );
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertThatAnSQLQueryHasBeenExecuted(Statistics stats) {
|
||||||
|
assertEquals( 1, stats.getQueryStatistics( HQL ).getExecutionCount() );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertThatNoSQLQueryHasBeenExecuted(Statistics stats) {
|
||||||
|
assertEquals( 0, stats.getQueryStatistics( HQL ).getExecutionCount() );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue