Skip to content

Commit b15433a

Browse files
cristatusSanne
authored andcommitted
HHH-14826 Add test to check regression caused by OneToOne cache support
1 parent 5e542b1 commit b15433a

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/OneToOneCacheTest.java

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package org.hibernate.test.onetoone.cache;
22

33
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;
47

58
import java.io.Serializable;
69
import java.lang.reflect.Constructor;
710
import java.util.ArrayList;
811
import java.util.List;
12+
import java.util.concurrent.atomic.AtomicLong;
913

1014
import org.hibernate.Session;
1115
import org.hibernate.Transaction;
@@ -17,6 +21,8 @@
1721
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
1822
import org.junit.Test;
1923

24+
import javax.persistence.*;
25+
2026
public class OneToOneCacheTest extends BaseCoreFunctionalTestCase {
2127
@Override
2228
public String[] getMappings() {
@@ -26,9 +32,18 @@ public String[] getMappings() {
2632
};
2733
}
2834

35+
@Override
36+
protected Class<?>[] getAnnotatedClasses() {
37+
return new Class[] {
38+
Product.class,
39+
ProductConfig.class
40+
};
41+
}
42+
2943
@Override
3044
protected void configure(Configuration configuration) {
3145
configuration.setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
46+
configuration.setProperty(AvailableSettings.JPA_SHARED_CACHE_MODE, "ENABLE_SELECTIVE");
3247
configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
3348
}
3449

@@ -117,4 +132,121 @@ public void OneToOneCacheByForeignKey() throws Exception {
117132
public void OneToOneCacheByRef() throws Exception {
118133
OneToOneTest(PersonByRef.class, DetailsByRef.class);
119134
}
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+
}
120252
}

0 commit comments

Comments
 (0)