Skip to content

Commit

Permalink
HHH-13640 - Uninitialized HibernateProxy mapped as NO_PROXY gets init…
Browse files Browse the repository at this point in the history
…ialized when reloaded with enhancement-as-proxy enabled

(cherry picked from commit ee30430)
  • Loading branch information
sebersole authored and gbadner committed Nov 7, 2019
1 parent fdb5ba7 commit ce24b3c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ private Object proxyOrLoad(
.getSessionFactoryOptions()
.isEnhancementAsProxyEnabled();

final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
final boolean entityHasHibernateProxyFactory = persister.getEntityMetamodel()
.getTuplizer()
.getProxyFactory() != null;
Expand Down Expand Up @@ -304,21 +305,21 @@ private Object proxyOrLoad(

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 ( persister.getEntityMetamodel().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.debugf( "Ignoring NO_PROXY for to-one association with subclasses to honor laziness" );
}
if ( entityMetamodel.hasSubclasses() ) {
// entities with subclasses that define a ProxyFactory can create a HibernateProxy
return createProxy( event, persister, keyToLoad, persistenceContext );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public LockOptions setScope(boolean scope) {
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 );
}
Expand Down Expand Up @@ -192,19 +190,4 @@ public PostLoadEvent getPostLoadEvent() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1156,11 +1156,11 @@ public final Object internalLoad(
LoadEvent event = loadEvent;
loadEvent = null;
event = recycleEventInstance( event, id, entityName );
event.setShouldUnwrapProxy( unwrapProxy );
fireLoadNoChecks( event, type );
Object result = event.getResult();
if ( !nullable ) {
UnresolvableObjectException.throwIfNull( result, id, entityName );}
UnresolvableObjectException.throwIfNull( result, id, entityName );
}

if ( loadEvent == null ) {
event.setEntityClassName( null );
Expand All @@ -1186,7 +1186,6 @@ private LoadEvent recycleEventInstance(final LoadEvent event, final Serializable
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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;
Expand Down Expand Up @@ -63,7 +62,6 @@ protected void applyMetadataSources(MetadataSources sources) {
}

@Test
@FailureExpected( jiraKey = "HHH-13640")
public void testNewProxyAssociation() {
inTransaction(
session -> {
Expand All @@ -87,7 +85,6 @@ public void testNewProxyAssociation() {
}

@Test
@FailureExpected( jiraKey = "HHH-13640")
public void testReusedProxyAssociation() {
inTransaction(
session -> {
Expand Down

0 comments on commit ce24b3c

Please sign in to comment.