HHH-13640 - Uninitialized HibernateProxy mapped as NO_PROXY gets initialized when reloaded with enhancement-as-proxy enabled

This commit is contained in:
Steve Ebersole 2019-09-30 17:50:39 -05:00
parent d46d3d66a2
commit ee304305e8
4 changed files with 8 additions and 31 deletions

View File

@ -287,21 +287,21 @@ public class DefaultLoadEventListener implements LoadEventListener {
LazyInitializer li = ( (HibernateProxy) proxy ).getHibernateLazyInitializer();
if ( li.isUnwrap() || event.getShouldUnwrapProxy() ) {
return li.getImplementation();
if ( li.isUnwrap() ) {
if ( entityMetamodel.hasSubclasses() ) {
LOG.debug( "Ignoring NO_PROXY for to-one association with subclasses to honor laziness" );
}
else {
return li.getImplementation();
}
}
return persistenceContext.narrowProxy( proxy, persister, keyToLoad, null );
}
// specialized handling for entities with subclasses with a HibernateProxy factory
if ( entityMetamodel.hasSubclasses() ) {
// entities with subclasses that define a ProxyFactory can create
// a HibernateProxy so long as NO_PROXY was not specified.
if ( event.getShouldUnwrapProxy() != null && event.getShouldUnwrapProxy() ) {
LOG.debug( "Ignoring NO_PROXY for to-one association with subclasses to honor laziness" );
}
// entities with subclasses that define a ProxyFactory can create a HibernateProxy
return createProxy( event, persister, keyToLoad, persistenceContext );
}
}

View File

@ -47,8 +47,6 @@ public class LoadEvent extends AbstractEvent {
private Object result;
private PostLoadEvent postLoadEvent;
private Boolean shouldUnwrapProxy;
public LoadEvent(Serializable entityId, Object instanceToLoad, EventSource source) {
this( entityId, null, instanceToLoad, DEFAULT_LOCK_OPTIONS, false, source );
}
@ -192,19 +190,4 @@ public class LoadEvent extends AbstractEvent {
public void setPostLoadEvent(PostLoadEvent postLoadEvent) {
this.postLoadEvent = postLoadEvent;
}
public Boolean getShouldUnwrapProxy() {
if ( shouldUnwrapProxy == null ) {
final boolean enabled = getSession().getFactory()
.getSessionFactoryOptions()
.isEnhancementAsProxyEnabled();
return enabled;
}
return shouldUnwrapProxy;
}
public void setShouldUnwrapProxy(Boolean shouldUnwrapProxy) {
this.shouldUnwrapProxy = shouldUnwrapProxy;
}
}

View File

@ -36,7 +36,6 @@ import javax.persistence.EntityNotFoundException;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceException;
import javax.persistence.PessimisticLockScope;
import javax.persistence.StoredProcedureQuery;
import javax.persistence.TransactionRequiredException;
import javax.persistence.criteria.CriteriaBuilder;
@ -1041,7 +1040,6 @@ public final class SessionImpl
loadEvent = null;
event = recycleEventInstance( event, id, entityName );
event.setShouldUnwrapProxy( unwrapProxy );
fireLoadNoChecks( event, type );
@ -1081,7 +1079,6 @@ public final class SessionImpl
event.setLockMode( LoadEvent.DEFAULT_LOCK_MODE );
event.setLockScope( LoadEvent.DEFAULT_LOCK_OPTIONS.getScope() );
event.setLockTimeout( LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut() );
event.setShouldUnwrapProxy( null );
return event;
}
}

View File

@ -22,7 +22,6 @@ import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
@ -63,7 +62,6 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction
}
@Test
@FailureExpected( jiraKey = "HHH-13640")
public void testNewProxyAssociation() {
inTransaction(
session -> {
@ -87,7 +85,6 @@ public class LazyToOnesProxyWithSubclassesTest extends BaseNonConfigCoreFunction
}
@Test
@FailureExpected( jiraKey = "HHH-13640")
public void testReusedProxyAssociation() {
inTransaction(
session -> {