Skip to content

Commit 1737ee4

Browse files
mbelladebeikov
authored andcommitted
HHH-17329 Add test for issue
1 parent 588408c commit 1737ee4

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/querycache/QueryCacheExistingEntityInstanceTest.java

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import java.util.function.Consumer;
1010

11+
import org.hibernate.annotations.Fetch;
12+
import org.hibernate.annotations.FetchMode;
1113
import org.hibernate.cfg.AvailableSettings;
1214
import org.hibernate.engine.spi.SessionImplementor;
1315
import org.hibernate.stat.spi.StatisticsImplementor;
@@ -18,33 +20,53 @@
1820
import org.hibernate.testing.orm.junit.SessionFactory;
1921
import org.hibernate.testing.orm.junit.SessionFactoryScope;
2022
import org.hibernate.testing.orm.junit.Setting;
23+
import org.junit.jupiter.api.AfterAll;
2124
import org.junit.jupiter.api.BeforeAll;
2225
import org.junit.jupiter.api.Test;
2326

2427
import jakarta.persistence.Cacheable;
2528
import jakarta.persistence.Entity;
29+
import jakarta.persistence.FetchType;
2630
import jakarta.persistence.Id;
31+
import jakarta.persistence.JoinColumn;
32+
import jakarta.persistence.ManyToOne;
2733

2834
import static org.assertj.core.api.Assertions.assertThat;
2935
import static org.junit.jupiter.api.Assertions.assertEquals;
3036

3137
/**
3238
* @author Marco Belladelli
3339
*/
34-
@DomainModel( annotatedClasses = QueryCacheExistingEntityInstanceTest.TestEntity.class )
40+
@DomainModel( annotatedClasses = {
41+
QueryCacheExistingEntityInstanceTest.TestEntity.class,
42+
QueryCacheExistingEntityInstanceTest.ParentEntity.class,
43+
} )
3544
@SessionFactory( generateStatistics = true )
3645
@ServiceRegistry( settings = {
3746
@Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "true" ),
3847
@Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" )
3948
} )
4049
@Jira( "https://hibernate.atlassian.net/browse/HHH-17188" )
50+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17329" )
4151
public class QueryCacheExistingEntityInstanceTest {
4252
private static final String TEXT = "text";
4353
private static final String QUERY = "select e from TestEntity e where text = :text";
4454

4555
@BeforeAll
4656
public void setUp(SessionFactoryScope scope) {
47-
scope.inTransaction( session -> session.persist( new TestEntity( 1L, TEXT ) ) );
57+
scope.inTransaction( session -> {
58+
final TestEntity testEntity = new TestEntity( 1L, TEXT );
59+
session.persist( testEntity );
60+
session.persist( new ParentEntity( 1L, testEntity ) );
61+
} );
62+
}
63+
64+
@AfterAll
65+
public void tearDown(SessionFactoryScope scope) {
66+
scope.inTransaction( session -> {
67+
session.createMutationQuery( "delete from ParentEntity" ).executeUpdate();
68+
session.createMutationQuery( "delete from TestEntity" ).executeUpdate();
69+
} );
4870
}
4971

5072
@Test
@@ -63,6 +85,27 @@ public void testAfterGetReference(SessionFactoryScope scope) {
6385
testQueryCache( scope, session -> session.getReference( TestEntity.class, 1L ) );
6486
}
6587

88+
@Test
89+
public void testAfterParentFind(SessionFactoryScope scope) {
90+
testQueryCache( scope, session -> {
91+
session.find( ParentEntity.class, 1L );
92+
// make TestEntity instance available in PC
93+
session.find( TestEntity.class, 1L );
94+
} );
95+
}
96+
97+
@Test
98+
public void testAfterParentQuery(SessionFactoryScope scope) {
99+
testQueryCache( scope, session -> {
100+
session.createQuery(
101+
"from ParentEntity",
102+
ParentEntity.class
103+
).getResultList();
104+
// make TestEntity instance available in PC
105+
session.find( TestEntity.class, 1L );
106+
} );
107+
}
108+
66109
private void testQueryCache(SessionFactoryScope scope, Consumer<SessionImplementor> beforeQuery) {
67110
scope.getSessionFactory().getCache().evictQueryRegions();
68111
final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics();
@@ -123,4 +166,27 @@ public String getText() {
123166
return text;
124167
}
125168
}
169+
170+
@Entity( name = "ParentEntity" )
171+
public static class ParentEntity {
172+
@Id
173+
private Long id;
174+
175+
@ManyToOne( fetch = FetchType.LAZY )
176+
@Fetch( FetchMode.SELECT )
177+
@JoinColumn( name = "test_entity_id" )
178+
private TestEntity testEntity;
179+
180+
public ParentEntity() {
181+
}
182+
183+
public ParentEntity(Long id, TestEntity testEntity) {
184+
this.id = id;
185+
this.testEntity = testEntity;
186+
}
187+
188+
public TestEntity getTestEntity() {
189+
return testEntity;
190+
}
191+
}
126192
}

0 commit comments

Comments
 (0)