1
1
package org .hibernate .test .onetoone .cache ;
2
2
3
3
import static org .junit .Assert .assertEquals ;
4
+ import static org .junit .Assert .assertTrue ;
5
+ import static org .junit .Assert .assertNotEquals ;
6
+ import static org .junit .Assert .assertNotNull ;
4
7
5
8
import java .io .Serializable ;
6
9
import java .lang .reflect .Constructor ;
7
10
import java .util .ArrayList ;
8
11
import java .util .List ;
12
+ import java .util .concurrent .atomic .AtomicLong ;
9
13
10
14
import org .hibernate .Session ;
11
15
import org .hibernate .Transaction ;
17
21
import org .hibernate .testing .junit4 .BaseCoreFunctionalTestCase ;
18
22
import org .junit .Test ;
19
23
24
+ import javax .persistence .*;
25
+
20
26
public class OneToOneCacheTest extends BaseCoreFunctionalTestCase {
21
27
@ Override
22
28
public String [] getMappings () {
@@ -26,9 +32,18 @@ public String[] getMappings() {
26
32
};
27
33
}
28
34
35
+ @ Override
36
+ protected Class <?>[] getAnnotatedClasses () {
37
+ return new Class [] {
38
+ Product .class ,
39
+ ProductConfig .class
40
+ };
41
+ }
42
+
29
43
@ Override
30
44
protected void configure (Configuration configuration ) {
31
45
configuration .setProperty (AvailableSettings .USE_SECOND_LEVEL_CACHE , "true" );
46
+ configuration .setProperty (AvailableSettings .JPA_SHARED_CACHE_MODE , "ENABLE_SELECTIVE" );
32
47
configuration .setProperty (AvailableSettings .GENERATE_STATISTICS , "true" );
33
48
}
34
49
@@ -117,4 +132,121 @@ public void OneToOneCacheByForeignKey() throws Exception {
117
132
public void OneToOneCacheByRef () throws Exception {
118
133
OneToOneTest (PersonByRef .class , DetailsByRef .class );
119
134
}
135
+
136
+ @ Test
137
+ public void testFieldShouldNotBeNull2 () {
138
+ final AtomicLong pid = new AtomicLong ();
139
+
140
+ // create Product
141
+ inTransaction (s -> {
142
+ Product product = new Product ();
143
+ s .persist (product );
144
+ pid .set (product .getId ());
145
+ });
146
+
147
+ // create ProductConfig and associate with a Product
148
+ inTransaction (s -> {
149
+ Product product = s .find (Product .class , pid .get ());
150
+ ProductConfig config = new ProductConfig ();
151
+ config .setProduct (product );
152
+ s .persist (config );
153
+ });
154
+
155
+ assertTrue (sessionFactory ().getCache ().containsEntity (Product .class , pid .get ()));
156
+
157
+ sessionFactory ().getStatistics ().clear ();
158
+
159
+ // now fetch the Product again
160
+ inTransaction (s -> {
161
+ Product product = s .find (Product .class , pid .get ());
162
+
163
+ // should have been from cache
164
+ assertNotEquals (0 , sessionFactory ().getStatistics ().getSecondLevelCacheHitCount ());
165
+
166
+ // this should not fail
167
+ assertNotNull ("one-to-one field should not be null" , product .getConfig ());
168
+ });
169
+ }
170
+
171
+ @ Entity (name = "Product" )
172
+ @ Cacheable
173
+ public static class Product {
174
+
175
+ @ Id
176
+ @ GeneratedValue
177
+ private Long id ;
178
+
179
+ @ Version
180
+ private Integer version ;
181
+
182
+ @ OneToOne (mappedBy = "product" , fetch = FetchType .LAZY , cascade = { CascadeType .PERSIST , CascadeType .MERGE })
183
+ private ProductConfig config ;
184
+
185
+ public Product () {}
186
+
187
+ public Long getId () {
188
+ return id ;
189
+ }
190
+
191
+ public void setId (Long id ) {
192
+ this .id = id ;
193
+ }
194
+
195
+ public Integer getVersion () {
196
+ return version ;
197
+ }
198
+
199
+ public void setVersion (Integer version ) {
200
+ this .version = version ;
201
+ }
202
+
203
+ public ProductConfig getConfig () {
204
+ return config ;
205
+ }
206
+
207
+ public void setConfig (ProductConfig config ) {
208
+ this .config = config ;
209
+ }
210
+ }
211
+
212
+ @ Entity (name = "ProductConfig" )
213
+ @ Cacheable
214
+ public static class ProductConfig {
215
+
216
+ @ Id
217
+ @ GeneratedValue
218
+ private Long id ;
219
+
220
+ @ Version
221
+ private Integer version ;
222
+
223
+ @ OneToOne (fetch = FetchType .LAZY , cascade = { CascadeType .PERSIST , CascadeType .MERGE })
224
+ private Product product ;
225
+
226
+ public ProductConfig () {}
227
+
228
+ public Long getId () {
229
+ return id ;
230
+ }
231
+
232
+ public void setId (Long id ) {
233
+ this .id = id ;
234
+ }
235
+
236
+ public Integer getVersion () {
237
+ return version ;
238
+ }
239
+
240
+ public void setVersion (Integer version ) {
241
+ this .version = version ;
242
+ }
243
+
244
+ public Product getProduct () {
245
+ return product ;
246
+ }
247
+
248
+ public void setProduct (Product product ) {
249
+ this .product = product ;
250
+ }
251
+ }
120
252
}
0 commit comments