HHH-14826 Add test to check regression caused by OneToOne cache support
This commit is contained in:
parent
5e542b16cb
commit
b15433a49c
|
@ -1,11 +1,15 @@
|
|||
package org.hibernate.test.onetoone.cache;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
@ -17,6 +21,8 @@ import org.hibernate.stat.spi.StatisticsImplementor;
|
|||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
public class OneToOneCacheTest extends BaseCoreFunctionalTestCase {
|
||||
@Override
|
||||
public String[] getMappings() {
|
||||
|
@ -26,9 +32,18 @@ public class OneToOneCacheTest extends BaseCoreFunctionalTestCase {
|
|||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] {
|
||||
Product.class,
|
||||
ProductConfig.class
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration configuration) {
|
||||
configuration.setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
|
||||
configuration.setProperty(AvailableSettings.JPA_SHARED_CACHE_MODE, "ENABLE_SELECTIVE");
|
||||
configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
|
||||
}
|
||||
|
||||
|
@ -117,4 +132,121 @@ public class OneToOneCacheTest extends BaseCoreFunctionalTestCase {
|
|||
public void OneToOneCacheByRef() throws Exception {
|
||||
OneToOneTest(PersonByRef.class, DetailsByRef.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldShouldNotBeNull2() {
|
||||
final AtomicLong pid = new AtomicLong();
|
||||
|
||||
// create Product
|
||||
inTransaction(s -> {
|
||||
Product product = new Product();
|
||||
s.persist(product);
|
||||
pid.set(product.getId());
|
||||
});
|
||||
|
||||
// create ProductConfig and associate with a Product
|
||||
inTransaction(s -> {
|
||||
Product product = s.find(Product.class, pid.get());
|
||||
ProductConfig config = new ProductConfig();
|
||||
config.setProduct(product);
|
||||
s.persist(config);
|
||||
});
|
||||
|
||||
assertTrue(sessionFactory().getCache().containsEntity(Product.class, pid.get()));
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
// now fetch the Product again
|
||||
inTransaction(s -> {
|
||||
Product product = s.find(Product.class, pid.get());
|
||||
|
||||
// should have been from cache
|
||||
assertNotEquals (0, sessionFactory().getStatistics().getSecondLevelCacheHitCount());
|
||||
|
||||
// this should not fail
|
||||
assertNotNull("one-to-one field should not be null", product.getConfig());
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Product")
|
||||
@Cacheable
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Version
|
||||
private Integer version;
|
||||
|
||||
@OneToOne(mappedBy = "product", fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
|
||||
private ProductConfig config;
|
||||
|
||||
public Product() {}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Integer version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public ProductConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setConfig(ProductConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "ProductConfig")
|
||||
@Cacheable
|
||||
public static class ProductConfig {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Version
|
||||
private Integer version;
|
||||
|
||||
@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
|
||||
private Product product;
|
||||
|
||||
public ProductConfig() {}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Integer version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Product getProduct() {
|
||||
return product;
|
||||
}
|
||||
|
||||
public void setProduct(Product product) {
|
||||
this.product = product;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue