From f0e0355ad832fe688ec9986d565f21c45b9770e0 Mon Sep 17 00:00:00 2001 From: Vlad Mihalcea Date: Mon, 5 Feb 2018 14:47:31 +0200 Subject: [PATCH] HHH-12273 - Load Proxy by its identifier should consider the Session UUID --- .../proxy/AbstractLazyInitializer.java | 12 +-- ...oadJpaComplianceDifferentSessionsTest.java | 82 +++++++++++++++++++ 2 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceDifferentSessionsTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java b/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java index 88c662783a..af440381b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java @@ -43,8 +43,6 @@ public abstract class AbstractLazyInitializer implements LazyInitializer { private String sessionFactoryUuid; private boolean allowLoadOutsideTransaction; - private boolean initializeProxyWhenAccessingIdentifier; - /** * For serialization from the non-pojo initializers (HHH-3309) */ @@ -67,8 +65,6 @@ public abstract class AbstractLazyInitializer implements LazyInitializer { } else { setSession( session ); - initializeProxyWhenAccessingIdentifier = session.getFactory().getSessionFactoryOptions() - .getJpaCompliance().isJpaProxyComplianceEnabled(); } } @@ -79,12 +75,18 @@ public abstract class AbstractLazyInitializer implements LazyInitializer { @Override public final Serializable getIdentifier() { - if ( isUninitialized() && initializeProxyWhenAccessingIdentifier ) { + if ( isUninitialized() && isInitializeProxyWhenAccessingIdentifier() ) { initialize(); } return id; } + private boolean isInitializeProxyWhenAccessingIdentifier() { + return session != null && session.getFactory() + .getSessionFactoryOptions() + .getJpaCompliance().isJpaProxyComplianceEnabled(); + } + @Override public final void setIdentifier(Serializable id) { this.id = id; diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceDifferentSessionsTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceDifferentSessionsTest.java new file mode 100644 index 0000000000..5b512f69d7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceDifferentSessionsTest.java @@ -0,0 +1,82 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.jpa.test.ops; + +import java.util.Map; +import javax.persistence.EntityManagerFactory; + +import org.hibernate.Session; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.jpa.boot.spi.Bootstrap; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.junit.Assert.assertEquals; + +/** + * @author Vlad Mihalcea + */ +@TestForIssue( jiraKey = "HHH-12273" ) +public class GetLoadJpaComplianceDifferentSessionsTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Workload.class, + }; + } + + @Override + @SuppressWarnings( "unchecked" ) + protected void addConfigOptions(Map options) { + options.put( AvailableSettings.JPA_PROXY_COMPLIANCE, Boolean.FALSE.toString() ); + } + + @Test + @TestForIssue( jiraKey = "HHH-9856" ) + public void testReattachEntityToSessionWithJpaComplianceProxy() { + final Integer _workloadId = doInJPA( this::entityManagerFactory, entityManager -> { + Workload workload = new Workload(); + workload.load = 123; + workload.name = "Package"; + entityManager.persist( workload ); + + return workload.getId(); + } ); + + Workload _workload = doInJPA( this::entityManagerFactory, entityManager -> { + return entityManager.getReference( Workload.class, _workloadId ); + } ); + + Map settings = buildSettings(); + settings.put( AvailableSettings.JPA_PROXY_COMPLIANCE, Boolean.TRUE.toString() ); + settings.put( AvailableSettings.HBM2DDL_AUTO, "none" ); + + EntityManagerFactory newEntityManagerFactory = Bootstrap + .getEntityManagerFactoryBuilder( + new TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ), + settings ) + .build(); + + try { + doInJPA( () -> newEntityManagerFactory, entityManager -> { + entityManager.unwrap( Session.class ).update( _workload ); + + _workload.getId(); + }); + } + finally { + newEntityManagerFactory.close(); + } + + assertEquals( "Package", _workload.getName() ); + } +} +