8
8
9
9
import org .hibernate .ObjectNotFoundException ;
10
10
import org .hibernate .Session ;
11
+ import org .hibernate .cache .internal .CollectionCacheInvalidator ;
12
+ import org .hibernate .cache .spi .access .CollectionRegionAccessStrategy ;
11
13
import org .hibernate .cfg .Configuration ;
12
14
import org .hibernate .cfg .Environment ;
13
-
14
- import org .junit . Test ;
15
+ import org . hibernate . engine . spi . SessionImplementor ;
16
+ import org .hibernate . persister . collection . CollectionPersister ;
15
17
16
18
import org .hibernate .testing .TestForIssue ;
17
19
import org .hibernate .testing .junit4 .BaseCoreFunctionalTestCase ;
20
+ import org .junit .Test ;
18
21
19
22
import static org .junit .Assert .assertEquals ;
23
+ import static org .junit .Assert .assertNotNull ;
24
+ import static org .junit .Assert .assertNull ;
20
25
import static org .junit .Assert .fail ;
21
26
22
27
/**
@@ -36,6 +41,7 @@ protected void configure(Configuration cfg) {
36
41
cfg .setProperty ( Environment .USE_SECOND_LEVEL_CACHE , "true" );
37
42
cfg .setProperty ( Environment .USE_QUERY_CACHE , "true" );
38
43
cfg .setProperty ( Environment .CACHE_PROVIDER_CONFIG , "true" );
44
+ cfg .setProperty ( CollectionCacheInvalidator .PROPAGATE_EXCEPTION , "true" );
39
45
}
40
46
41
47
@ Override
@@ -68,6 +74,31 @@ protected void cleanupTest() throws Exception {
68
74
s .close ();
69
75
}
70
76
77
+ @ Test
78
+ public void testCachedValueAfterEviction () {
79
+ CollectionPersister persister = sessionFactory ().getCollectionPersister ( Company .class .getName () + ".users" );
80
+
81
+ Session session = openSession ();
82
+ SessionImplementor sessionImplementor = (SessionImplementor ) session ;
83
+
84
+ CollectionRegionAccessStrategy cache = persister .getCacheAccessStrategy ();
85
+ Object key = cache .generateCacheKey ( 1 , persister , sessionFactory (), session .getTenantIdentifier () );
86
+ Object cachedValue = cache .get ( sessionImplementor , key , sessionImplementor .getTimestamp () );
87
+ assertNull ( cachedValue );
88
+
89
+ Company company = session .get ( Company .class , 1 );
90
+ //should add in cache
91
+ assertEquals ( 1 , company .getUsers ().size () );
92
+ session .close ();
93
+
94
+ session = openSession ();
95
+ sessionImplementor = (SessionImplementor ) session ;
96
+ key = cache .generateCacheKey ( 1 , persister , sessionFactory (), session .getTenantIdentifier () );
97
+ cachedValue = cache .get ( sessionImplementor , key , sessionImplementor .getTimestamp () );
98
+ assertNotNull ( "Collection wasn't cached" , cachedValue );
99
+ session .close ();
100
+ }
101
+
71
102
@ Test
72
103
public void testCollectionCacheEvictionInsert () {
73
104
Session s = openSession ();
@@ -93,6 +124,29 @@ public void testCollectionCacheEvictionInsert() {
93
124
s .close ();
94
125
}
95
126
127
+ @ Test
128
+ public void testCollectionCacheEvictionInsertWithEntityOutOfContext () {
129
+ Session s = openSession ();
130
+ Company company = s .get ( Company .class , 1 );
131
+ assertEquals ( 1 , company .getUsers ().size () );
132
+ s .close ();
133
+
134
+ s = openSession ();
135
+ s .beginTransaction ();
136
+
137
+ User user = new User ( 2 , company );
138
+ s .save ( user );
139
+
140
+ s .getTransaction ().commit ();
141
+ s .close ();
142
+
143
+ s = openSession ();
144
+
145
+ company = s .get ( Company .class , 1 );
146
+ assertEquals ( 2 , company .getUsers ().size () );
147
+ s .close ();
148
+ }
149
+
96
150
@ Test
97
151
public void testCollectionCacheEvictionRemove () {
98
152
Session s = openSession ();
@@ -121,6 +175,32 @@ public void testCollectionCacheEvictionRemove() {
121
175
s .close ();
122
176
}
123
177
178
+ @ Test
179
+ public void testCollectionCacheEvictionRemoveWithEntityOutOfContext () {
180
+ Session s = openSession ();
181
+ Company company = s .get ( Company .class , 1 );
182
+ assertEquals ( 1 , company .getUsers ().size () );
183
+ s .close ();
184
+
185
+ s = openSession ();
186
+ s .beginTransaction ();
187
+ s .delete ( company .getUsers ().get ( 0 ) );
188
+
189
+ s .getTransaction ().commit ();
190
+ s .close ();
191
+
192
+ s = openSession ();
193
+
194
+ company = s .get ( Company .class , 1 );
195
+ try {
196
+ assertEquals ( 0 , company .getUsers ().size () );
197
+ }
198
+ catch ( ObjectNotFoundException e ) {
199
+ fail ( "Cached element not found" );
200
+ }
201
+ s .close ();
202
+ }
203
+
124
204
@ Test
125
205
public void testCollectionCacheEvictionUpdate () {
126
206
Session s = openSession ();
@@ -157,4 +237,39 @@ public void testCollectionCacheEvictionUpdate() {
157
237
158
238
s .close ();
159
239
}
240
+
241
+ @ Test
242
+ public void testCollectionCacheEvictionUpdateWithEntityOutOfContext () {
243
+ Session s = openSession ();
244
+ Company company1 = s .get ( Company .class , 1 );
245
+ Company company2 = s .get ( Company .class , 2 );
246
+
247
+ assertEquals ( 1 , company1 .getUsers ().size () );
248
+ assertEquals ( 0 , company2 .getUsers ().size () );
249
+
250
+ s .close ();
251
+ s = openSession ();
252
+ s .beginTransaction ();
253
+
254
+ User user = s .get ( User .class , 1 );
255
+ user .setCompany ( company2 );
256
+
257
+ s .getTransaction ().commit ();
258
+ s .close ();
259
+
260
+ s = openSession ();
261
+
262
+ company1 = s .get ( Company .class , 1 );
263
+ company2 = s .get ( Company .class , 2 );
264
+
265
+ assertEquals ( 1 , company2 .getUsers ().size () );
266
+
267
+ try {
268
+ assertEquals ( 0 , company1 .getUsers ().size () );
269
+ }
270
+ catch ( ObjectNotFoundException e ) {
271
+ fail ( "Cached element not found" );
272
+ }
273
+ s .close ();
274
+ }
160
275
}
0 commit comments