Skip to content

HHH-14826 - Regression: OneToOne fields are always null if parent is loaded from L2 cache #4228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ public Serializable disassemble(Object value, SharedSessionContractImplementor s

@Override
public Object assemble(Serializable oid, SharedSessionContractImplementor session, Object owner) throws HibernateException {

if ( oid == null ) {
if ( uniqueKeyPropertyName != null ) {
return resolve( session.getContextEntityIdentifier( owner ), session, owner );
}
return null;
}

//the owner of the association is not the owner of the id
Serializable id = ( Serializable ) getIdentifierType( session ).assemble( oid, session, null );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.hibernate.test.onetoone.cache;

import java.util.concurrent.atomic.AtomicLong;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Version;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@TestForIssue( jiraKey = "HHH-14826")
public class OneToOneCacheEnableSelectingTest 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");
}

@Test
public void testFieldShouldNotBeNull() {
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;
}
}

}