8
8
9
9
import java .util .function .Consumer ;
10
10
11
+ import org .hibernate .annotations .Fetch ;
12
+ import org .hibernate .annotations .FetchMode ;
11
13
import org .hibernate .cfg .AvailableSettings ;
12
14
import org .hibernate .engine .spi .SessionImplementor ;
13
15
import org .hibernate .stat .spi .StatisticsImplementor ;
18
20
import org .hibernate .testing .orm .junit .SessionFactory ;
19
21
import org .hibernate .testing .orm .junit .SessionFactoryScope ;
20
22
import org .hibernate .testing .orm .junit .Setting ;
23
+ import org .junit .jupiter .api .AfterAll ;
21
24
import org .junit .jupiter .api .BeforeAll ;
22
25
import org .junit .jupiter .api .Test ;
23
26
24
27
import jakarta .persistence .Cacheable ;
25
28
import jakarta .persistence .Entity ;
29
+ import jakarta .persistence .FetchType ;
26
30
import jakarta .persistence .Id ;
31
+ import jakarta .persistence .JoinColumn ;
32
+ import jakarta .persistence .ManyToOne ;
27
33
28
34
import static org .assertj .core .api .Assertions .assertThat ;
29
35
import static org .junit .jupiter .api .Assertions .assertEquals ;
30
36
31
37
/**
32
38
* @author Marco Belladelli
33
39
*/
34
- @ DomainModel ( annotatedClasses = QueryCacheExistingEntityInstanceTest .TestEntity .class )
40
+ @ DomainModel ( annotatedClasses = {
41
+ QueryCacheExistingEntityInstanceTest .TestEntity .class ,
42
+ QueryCacheExistingEntityInstanceTest .ParentEntity .class ,
43
+ } )
35
44
@ SessionFactory ( generateStatistics = true )
36
45
@ ServiceRegistry ( settings = {
37
46
@ Setting ( name = AvailableSettings .USE_QUERY_CACHE , value = "true" ),
38
47
@ Setting ( name = AvailableSettings .USE_SECOND_LEVEL_CACHE , value = "true" )
39
48
} )
40
49
@ Jira ( "https://hibernate.atlassian.net/browse/HHH-17188" )
50
+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-17329" )
41
51
public class QueryCacheExistingEntityInstanceTest {
42
52
private static final String TEXT = "text" ;
43
53
private static final String QUERY = "select e from TestEntity e where text = :text" ;
44
54
45
55
@ BeforeAll
46
56
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
+ } );
48
70
}
49
71
50
72
@ Test
@@ -63,6 +85,27 @@ public void testAfterGetReference(SessionFactoryScope scope) {
63
85
testQueryCache ( scope , session -> session .getReference ( TestEntity .class , 1L ) );
64
86
}
65
87
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
+
66
109
private void testQueryCache (SessionFactoryScope scope , Consumer <SessionImplementor > beforeQuery ) {
67
110
scope .getSessionFactory ().getCache ().evictQueryRegions ();
68
111
final StatisticsImplementor statistics = scope .getSessionFactory ().getStatistics ();
@@ -123,4 +166,27 @@ public String getText() {
123
166
return text ;
124
167
}
125
168
}
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
+ }
126
192
}
0 commit comments