HHH-17329 Add test for issue

This commit is contained in:
Marco Belladelli 2023-10-18 10:07:29 +02:00 committed by Christian Beikov
parent 75744568e5
commit 6c28f9edc1
1 changed files with 68 additions and 2 deletions

View File

@ -8,6 +8,8 @@ package org.hibernate.orm.test.querycache;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.stat.spi.StatisticsImplementor; import org.hibernate.stat.spi.StatisticsImplementor;
@ -18,12 +20,16 @@ import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting; import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import jakarta.persistence.Cacheable; import jakarta.persistence.Cacheable;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -31,20 +37,36 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* @author Marco Belladelli * @author Marco Belladelli
*/ */
@DomainModel( annotatedClasses = QueryCacheExistingEntityInstanceTest.TestEntity.class ) @DomainModel( annotatedClasses = {
QueryCacheExistingEntityInstanceTest.TestEntity.class,
QueryCacheExistingEntityInstanceTest.ParentEntity.class,
} )
@SessionFactory( generateStatistics = true ) @SessionFactory( generateStatistics = true )
@ServiceRegistry( settings = { @ServiceRegistry( settings = {
@Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "true" ), @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "true" ),
@Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ) @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" )
} ) } )
@Jira( "https://hibernate.atlassian.net/browse/HHH-17188" ) @Jira( "https://hibernate.atlassian.net/browse/HHH-17188" )
@Jira( "https://hibernate.atlassian.net/browse/HHH-17329" )
public class QueryCacheExistingEntityInstanceTest { public class QueryCacheExistingEntityInstanceTest {
private static final String TEXT = "text"; private static final String TEXT = "text";
private static final String QUERY = "select e from TestEntity e where text = :text"; private static final String QUERY = "select e from TestEntity e where text = :text";
@BeforeAll @BeforeAll
public void setUp(SessionFactoryScope scope) { public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> session.persist( new TestEntity( 1L, TEXT ) ) ); scope.inTransaction( session -> {
final TestEntity testEntity = new TestEntity( 1L, TEXT );
session.persist( testEntity );
session.persist( new ParentEntity( 1L, testEntity ) );
} );
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.createMutationQuery( "delete from ParentEntity" ).executeUpdate();
session.createMutationQuery( "delete from TestEntity" ).executeUpdate();
} );
} }
@Test @Test
@ -63,6 +85,27 @@ public class QueryCacheExistingEntityInstanceTest {
testQueryCache( scope, session -> session.getReference( TestEntity.class, 1L ) ); testQueryCache( scope, session -> session.getReference( TestEntity.class, 1L ) );
} }
@Test
public void testAfterParentFind(SessionFactoryScope scope) {
testQueryCache( scope, session -> {
session.find( ParentEntity.class, 1L );
// make TestEntity instance available in PC
session.find( TestEntity.class, 1L );
} );
}
@Test
public void testAfterParentQuery(SessionFactoryScope scope) {
testQueryCache( scope, session -> {
session.createQuery(
"from ParentEntity",
ParentEntity.class
).getResultList();
// make TestEntity instance available in PC
session.find( TestEntity.class, 1L );
} );
}
private void testQueryCache(SessionFactoryScope scope, Consumer<SessionImplementor> beforeQuery) { private void testQueryCache(SessionFactoryScope scope, Consumer<SessionImplementor> beforeQuery) {
scope.getSessionFactory().getCache().evictQueryRegions(); scope.getSessionFactory().getCache().evictQueryRegions();
final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
@ -123,4 +166,27 @@ public class QueryCacheExistingEntityInstanceTest {
return text; return text;
} }
} }
@Entity( name = "ParentEntity" )
public static class ParentEntity {
@Id
private Long id;
@ManyToOne( fetch = FetchType.LAZY )
@Fetch( FetchMode.SELECT )
@JoinColumn( name = "test_entity_id" )
private TestEntity testEntity;
public ParentEntity() {
}
public ParentEntity(Long id, TestEntity testEntity) {
this.id = id;
this.testEntity = testEntity;
}
public TestEntity getTestEntity() {
return testEntity;
}
}
} }