From 33ce6a3d79396b93d259610befc9db3123c114e8 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Thu, 1 Dec 2022 17:00:36 +0000 Subject: [PATCH] HHH-15790 Refactor casting strategies for HibernateProxy across the codebase --- .../main/java/org/hibernate/Hibernate.java | 14 ++-- .../hibernate/engine/internal/Cascade.java | 4 +- .../internal/StatefulPersistenceContext.java | 74 +++++++++---------- .../engine/spi/CascadingActions.java | 4 +- .../internal/DefaultMergeEventListener.java | 3 +- .../org/hibernate/internal/SessionImpl.java | 21 ++---- .../internal/StatelessSessionImpl.java | 5 +- .../access/IdentifierLoadAccessImpl.java | 9 +-- .../java/org/hibernate/type/EntityType.java | 6 +- .../event/spi/BaseEnversEventListener.java | 12 ++- .../internal/entities/EntityInstantiator.java | 9 +-- .../entities/mapper/id/SingleIdMapper.java | 13 ++-- .../id/VirtualEntitySingleIdMapper.java | 7 +- .../envers/internal/tools/EntityTools.java | 23 +++--- 14 files changed, 97 insertions(+), 107 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/Hibernate.java b/hibernate-core/src/main/java/org/hibernate/Hibernate.java index f63ce225b7..b945f79474 100644 --- a/hibernate-core/src/main/java/org/hibernate/Hibernate.java +++ b/hibernate-core/src/main/java/org/hibernate/Hibernate.java @@ -35,9 +35,7 @@ import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.LazyInitializer; import org.hibernate.collection.spi.LazyInitializable; -import static org.hibernate.engine.internal.ManagedTypeHelper.asHibernateProxy; import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable; -import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy; import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable; /** @@ -128,8 +126,9 @@ public final class Hibernate { return; } - if ( isHibernateProxy( proxy ) ) { - asHibernateProxy( proxy ).getHibernateLazyInitializer().initialize(); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy ); + if ( lazyInitializer != null ) { + lazyInitializer.initialize(); } else if ( proxy instanceof LazyInitializable ) { ( (LazyInitializable) proxy ).forceInitialization(); @@ -319,10 +318,9 @@ public final class Hibernate { * uninitialized proxy that is not associated with an open session. */ public static Object unproxy(Object proxy) { - if ( isHibernateProxy( proxy ) ) { - HibernateProxy hibernateProxy = asHibernateProxy( proxy ); - LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer(); - return initializer.getImplementation(); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy ); + if ( lazyInitializer != null ) { + return lazyInitializer.getImplementation(); } else { return proxy; diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java index 662c8f2c9d..7d20000b4d 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java @@ -36,6 +36,8 @@ import org.hibernate.type.EntityType; import org.hibernate.type.ForeignKeyDirection; import org.hibernate.type.Type; +import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy; + /** * Delegate responsible for, in conjunction with the various * {@linkplain CascadingAction actions}, implementing cascade processing. @@ -320,7 +322,7 @@ public final class Cascade { if ( child == null || ( loadedValue != null && child != loadedValue ) ) { EntityEntry valueEntry = persistenceContext.getEntry( loadedValue ); - if ( valueEntry == null && loadedValue instanceof HibernateProxy ) { + if ( valueEntry == null && isHibernateProxy( loadedValue ) ) { // un-proxy and re-associate for cascade operation // useful for @OneToOne defined as FetchType.LAZY loadedValue = persistenceContext.unproxyAndReassociate( loadedValue ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java index 0023b5b80a..c64266ba14 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java @@ -602,10 +602,9 @@ public class StatefulPersistenceContext implements PersistenceContext { if ( ! Hibernate.isInitialized( value ) ) { // could be a proxy.... - if ( isHibernateProxy( value ) ) { - final HibernateProxy proxy = asHibernateProxy( value ); - final LazyInitializer li = proxy.getHibernateLazyInitializer(); - reassociateProxy( li, proxy ); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( value ); + if ( lazyInitializer != null ) { + reassociateProxy( lazyInitializer, asHibernateProxy( value ) ); return true; } @@ -626,12 +625,11 @@ public class StatefulPersistenceContext implements PersistenceContext { @Override public void reassociateProxy(Object value, Object id) throws MappingException { - if ( isHibernateProxy( value ) ) { + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( value ); + if ( lazyInitializer != null ) { LOG.debugf( "Setting proxy identifier: %s", id ); - final HibernateProxy proxy = asHibernateProxy( value ); - final LazyInitializer li = proxy.getHibernateLazyInitializer(); - li.setIdentifier( id ); - reassociateProxy( li, proxy ); + lazyInitializer.setIdentifier( id ); + reassociateProxy( lazyInitializer, asHibernateProxy( value ) ); } } @@ -656,16 +654,15 @@ public class StatefulPersistenceContext implements PersistenceContext { @Override public Object unproxy(Object maybeProxy) throws HibernateException { - if ( isHibernateProxy( maybeProxy ) ) { - final HibernateProxy proxy = asHibernateProxy( maybeProxy ); - final LazyInitializer li = proxy.getHibernateLazyInitializer(); - if ( li.isUninitialized() ) { + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( maybeProxy ); + if ( lazyInitializer != null ) { + if ( lazyInitializer.isUninitialized() ) { throw new PersistentObjectException( - "object was an uninitialized proxy for " + li.getEntityName() + "object was an uninitialized proxy for " + lazyInitializer.getEntityName() ); } //unwrap the object and return - return li.getImplementation(); + return lazyInitializer.getImplementation(); } else { return maybeProxy; @@ -673,13 +670,12 @@ public class StatefulPersistenceContext implements PersistenceContext { } @Override - public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException { - if ( isHibernateProxy( maybeProxy ) ) { - final HibernateProxy proxy = asHibernateProxy( maybeProxy ); - final LazyInitializer li = proxy.getHibernateLazyInitializer(); - reassociateProxy( li, proxy ); + public Object unproxyAndReassociate(final Object maybeProxy) throws HibernateException { + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( maybeProxy ); + if ( lazyInitializer != null ) { + reassociateProxy( lazyInitializer, asHibernateProxy( maybeProxy ) ); //initialize + unwrap the object and return it - return li.getImplementation(); + return lazyInitializer.getImplementation(); } else if ( isPersistentAttributeInterceptable( maybeProxy ) ) { final PersistentAttributeInterceptable interceptable = asPersistentAttributeInterceptable( maybeProxy ); @@ -724,9 +720,9 @@ public class StatefulPersistenceContext implements PersistenceContext { // Similarly, if the original HibernateProxy is initialized, there // is again no point in creating a proxy. Just return the impl - final HibernateProxy originalHibernateProxy = asHibernateProxy( proxy ); - if ( !originalHibernateProxy.getHibernateLazyInitializer().isUninitialized() ) { - final Object impl = originalHibernateProxy.getHibernateLazyInitializer().getImplementation(); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy ); + if ( !lazyInitializer.isUninitialized() ) { + final Object impl = lazyInitializer.getImplementation(); // can we return it? if ( concreteProxyClass.isInstance( impl ) ) { removeProxyByKey( key ); @@ -739,15 +735,14 @@ public class StatefulPersistenceContext implements PersistenceContext { final HibernateProxy narrowedProxy = asHibernateProxy( persister.createProxy( key.getIdentifier(), session ) ); // set the read-only/modifiable mode in the new proxy to what it was in the original proxy - final boolean readOnlyOrig = originalHibernateProxy.getHibernateLazyInitializer().isReadOnly(); + final boolean readOnlyOrig = lazyInitializer.isReadOnly(); narrowedProxy.getHibernateLazyInitializer().setReadOnly( readOnlyOrig ); return narrowedProxy; } else { if ( object != null ) { - final LazyInitializer li = asHibernateProxy( proxy ).getHibernateLazyInitializer(); - li.setImplementation( object ); + HibernateProxy.extractLazyInitializer( proxy ).setImplementation( object ); } return proxy; } @@ -1318,9 +1313,10 @@ public class StatefulPersistenceContext implements PersistenceContext { if ( mergeMap != null ) { for ( Object o : mergeMap.entrySet() ) { final Entry mergeMapEntry = (Entry) o; - if ( isHibernateProxy( mergeMapEntry.getKey() ) ) { - final HibernateProxy proxy = asHibernateProxy( mergeMapEntry.getKey() ); - if ( persister.isSubclassEntityName( proxy.getHibernateLazyInitializer().getEntityName() ) ) { + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( mergeMapEntry.getKey() ); + if ( lazyInitializer != null ) { + if ( persister.isSubclassEntityName( lazyInitializer.getEntityName() ) ) { + HibernateProxy proxy = asHibernateProxy( mergeMapEntry.getKey() ); boolean found = isFoundInParent( propertyName, childEntity, @@ -1504,12 +1500,12 @@ public class StatefulPersistenceContext implements PersistenceContext { if ( isReadOnly( object ) == readOnly ) { return; } - if ( isHibernateProxy( object ) ) { - final HibernateProxy proxy = asHibernateProxy( object ); - setProxyReadOnly( proxy, readOnly ); - if ( Hibernate.isInitialized( proxy ) ) { + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( object ); + if ( lazyInitializer != null ) { + setProxyReadOnly( lazyInitializer, readOnly ); + if ( ! lazyInitializer.isUninitialized() ) { setEntityReadOnly( - proxy.getHibernateLazyInitializer().getImplementation(), + lazyInitializer.getImplementation(), readOnly ); } @@ -1519,14 +1515,14 @@ public class StatefulPersistenceContext implements PersistenceContext { // PersistenceContext.proxyFor( entity ) returns entity if there is no proxy for that entity // so need to check the return value to be sure it is really a proxy final Object maybeProxy = getSession().getPersistenceContextInternal().proxyFor( object ); - if ( isHibernateProxy( maybeProxy ) ) { - setProxyReadOnly( asHibernateProxy( maybeProxy ), readOnly ); + final LazyInitializer lazyInitializer1 = HibernateProxy.extractLazyInitializer( maybeProxy ); + if ( lazyInitializer1 != null ) { + setProxyReadOnly( lazyInitializer1, readOnly ); } } } - private void setProxyReadOnly(HibernateProxy proxy, boolean readOnly) { - final LazyInitializer hibernateLazyInitializer = proxy.getHibernateLazyInitializer(); + private void setProxyReadOnly(LazyInitializer hibernateLazyInitializer, boolean readOnly) { if ( hibernateLazyInitializer.getSession() != getSession() ) { throw new AssertionFailure( "Attempt to set a proxy to read-only that is associated with a different session" ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java index 5c6e9ce983..12bf053734 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java @@ -29,6 +29,8 @@ import org.hibernate.type.Type; import org.jboss.logging.Logger; +import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy; + /** * @author Steve Ebersole */ @@ -361,7 +363,7 @@ public class CascadingActions { Object child = persister.getValue( parent, propertyIndex ); if ( child != null && !isInManagedState( child, session ) - && !(child instanceof HibernateProxy) ) { //a proxy cannot be transient and it breaks ForeignKeys.isTransient + && !( isHibernateProxy( child ) ) ) { //a proxy cannot be transient and it breaks ForeignKeys.isTransient final String childEntityName = ((EntityType) propertyType).getAssociatedEntityName( session.getFactory() ); if ( ForeignKeys.isTransient(childEntityName, child, null, session) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java index 45dab9d6f0..106749639d 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java @@ -48,6 +48,7 @@ import org.hibernate.type.TypeHelper; import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable; import static org.hibernate.engine.internal.ManagedTypeHelper.asSelfDirtinessTracker; +import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy; import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable; import static org.hibernate.engine.internal.ManagedTypeHelper.isSelfDirtinessTracker; @@ -394,7 +395,7 @@ public class DefaultMergeEventListener Object managed, EntityPersister persister, EventSource source) { - if ( managed instanceof HibernateProxy ) { + if ( isHibernateProxy( managed ) ) { return source.getPersistenceContextInternal().unproxy( managed ); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 4f8dd5f33e..a79ceda910 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -1522,22 +1522,13 @@ public class SessionImpl @Override public Object getContextEntityIdentifier(Object object) { checkOpenOrWaitingForAutoClose(); - if ( object instanceof HibernateProxy ) { - return getProxyIdentifier( object ); - } - else { - EntityEntry entry = persistenceContext.getEntry( object ); - return entry != null ? entry.getId() : null; - } - } - - private Object getProxyIdentifier(Object proxy) { - final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy ); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( object ); if ( lazyInitializer != null ) { return lazyInitializer.getInternalIdentifier(); } else { - throw new HibernateException( "Argument was not an HibernateProxy, which is a requirement for this method" ); + EntityEntry entry = persistenceContext.getEntry( object ); + return entry != null ? entry.getId() : null; } } @@ -1578,7 +1569,7 @@ public class SessionImpl delayedAfterCompletion(); if ( entry == null ) { - if ( ! ( isHibernateProxy( object ) ) && persistenceContext.getEntry( object ) == null ) { + if ( ! ( lazyInitializer != null ) && persistenceContext.getEntry( object ) == null ) { // check if it is even an entity -> if not throw an exception (per JPA) try { final String entityName = getEntityNameResolver().resolveEntityName( object ); @@ -1617,7 +1608,8 @@ public class SessionImpl } try { - if ( !(object instanceof HibernateProxy) && persistenceContext.getEntry( object ) == null ) { + final LazyInitializer li = HibernateProxy.extractLazyInitializer( object ); + if ( ! ( li != null ) && persistenceContext.getEntry( object ) == null ) { // check if it is an entity -> if not throw an exception (per JPA) try { getFactory().getRuntimeMetamodels() @@ -1629,7 +1621,6 @@ public class SessionImpl } } - final LazyInitializer li = HibernateProxy.extractLazyInitializer( object ); if ( li != null ) { //do not use proxiesByKey, since not all //proxies that point to this session's diff --git a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java index ec3a5b03f3..a5590458b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java @@ -38,6 +38,7 @@ import org.hibernate.tuple.entity.EntityMetamodel; import jakarta.transaction.SystemException; import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable; +import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy; import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable; /** @@ -386,8 +387,8 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen public void fetch(Object association) { checkOpen(); PersistenceContext persistenceContext = getPersistenceContext(); - if ( association instanceof HibernateProxy ) { - final LazyInitializer initializer = HibernateProxy.extractLazyInitializer( association ); + final LazyInitializer initializer = HibernateProxy.extractLazyInitializer( association ); + if ( initializer != null ) { if ( initializer.isUninitialized() ) { String entityName = initializer.getEntityName(); Object id = initializer.getIdentifier(); diff --git a/hibernate-core/src/main/java/org/hibernate/loader/access/IdentifierLoadAccessImpl.java b/hibernate-core/src/main/java/org/hibernate/loader/access/IdentifierLoadAccessImpl.java index ab17fe24b5..9d915d2a09 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/access/IdentifierLoadAccessImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/access/IdentifierLoadAccessImpl.java @@ -213,11 +213,10 @@ public class IdentifierLoadAccessImpl implements IdentifierLoadAccess, Jav return; } - if ( isHibernateProxy( result ) ) { - final HibernateProxy hibernateProxy = asHibernateProxy( result ); - final LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer(); - if ( initializer.isUninitialized() ) { - initializer.initialize(); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( result ); + if ( lazyInitializer != null ) { + if ( lazyInitializer.isUninitialized() ) { + lazyInitializer.initialize(); } } else { diff --git a/hibernate-core/src/main/java/org/hibernate/type/EntityType.java b/hibernate-core/src/main/java/org/hibernate/type/EntityType.java index b8b155ff43..a7a54cf9b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/EntityType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/EntityType.java @@ -510,9 +510,9 @@ public abstract class EntityType extends AbstractType implements AssociationType if ( persister.hasIdentifierProperty() ) { final Object id; - if ( isHibernateProxy( value ) ) { - HibernateProxy proxy = asHibernateProxy( value ); - id = proxy.getHibernateLazyInitializer().getInternalIdentifier(); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( value ); + if ( lazyInitializer != null ) { + id = lazyInitializer.getInternalIdentifier(); } else { id = persister.getIdentifier( value, null ); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/event/spi/BaseEnversEventListener.java b/hibernate-envers/src/main/java/org/hibernate/envers/event/spi/BaseEnversEventListener.java index 8d6bd06c6d..2ecd49ff18 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/event/spi/BaseEnversEventListener.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/event/spi/BaseEnversEventListener.java @@ -19,9 +19,7 @@ import org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUn import org.hibernate.envers.internal.tools.EntityTools; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.proxy.HibernateProxy; - -import static org.hibernate.engine.internal.ManagedTypeHelper.asHibernateProxy; -import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy; +import org.hibernate.proxy.LazyInitializer; /** * Base class for all Envers event listeners @@ -95,11 +93,11 @@ public abstract class BaseEnversEventListener implements EnversListener { String toEntityName; Object id; - if ( isHibernateProxy( value ) ) { - final HibernateProxy hibernateProxy = asHibernateProxy( value ); - id = hibernateProxy.getHibernateLazyInitializer().getInternalIdentifier(); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( value ); + if ( lazyInitializer != null ) { + id = lazyInitializer.getInternalIdentifier(); // We've got to initialize the object from the proxy to later read its state. - value = EntityTools.getTargetFromProxy( session.getFactory(), hibernateProxy ); + value = EntityTools.getTargetFromProxy( session.getFactory(), lazyInitializer ); // HHH-7249 // This call must occur after the proxy has been initialized or the returned name will // be to the base class which will impact the discriminator value chosen when using an diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/EntityInstantiator.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/EntityInstantiator.java index 3f80d442e7..87f404f688 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/EntityInstantiator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/EntityInstantiator.java @@ -108,11 +108,10 @@ public class EntityInstantiator { final Map originalId = (Map) versionsEntity.get( enversService.getConfig().getOriginalIdPropertyName() ); for ( Object key : originalId.keySet() ) { final Object value = originalId.get( key ); - if ( isHibernateProxy( value ) ) { - final HibernateProxy hibernateProxy = asHibernateProxy( value ); - final LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer(); - final String entityName = initializer.getEntityName(); - final Object entityId = initializer.getInternalIdentifier(); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( value ); + if ( lazyInitializer != null ) { + final String entityName = lazyInitializer.getEntityName(); + final Object entityId = lazyInitializer.getInternalIdentifier(); if ( enversService.getEntitiesConfigurations().isVersioned( entityName ) ) { final String entityClassName = enversService.getEntitiesConfigurations().get( entityName ).getEntityClassName(); final Class entityClass = ReflectionTools.loadClass( diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/SingleIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/SingleIdMapper.java index b3b65425eb..d12368f374 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/SingleIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/SingleIdMapper.java @@ -13,6 +13,7 @@ import java.util.Map; import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.internal.entities.PropertyData; import org.hibernate.proxy.HibernateProxy; +import org.hibernate.proxy.LazyInitializer; import org.hibernate.service.ServiceRegistry; import static org.hibernate.engine.internal.ManagedTypeHelper.asHibernateProxy; @@ -80,9 +81,9 @@ public class SingleIdMapper extends AbstractIdMapper implements SimpleIdMapperBu return null; } - if ( isHibernateProxy( data ) ) { - final HibernateProxy hibernateProxy = asHibernateProxy( data ); - return hibernateProxy.getHibernateLazyInitializer().getInternalIdentifier(); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( data ); + if ( lazyInitializer != null ) { + return lazyInitializer.getInternalIdentifier(); } else { return getValueFromObject( propertyData, data ); @@ -102,9 +103,9 @@ public class SingleIdMapper extends AbstractIdMapper implements SimpleIdMapperBu data.put( propertyData.getName(), null ); } else { - if ( isHibernateProxy( obj ) ) { - final HibernateProxy hibernateProxy = asHibernateProxy( obj ); - data.put( propertyData.getName(), hibernateProxy.getHibernateLazyInitializer().getInternalIdentifier() ); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( obj ); + if ( lazyInitializer != null ) { + data.put( propertyData.getName(), lazyInitializer.getInternalIdentifier() ); } else { final Object value = getValueFromObject( propertyData, obj ); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/VirtualEntitySingleIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/VirtualEntitySingleIdMapper.java index 2e77cf9aa1..b0edd27bd4 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/VirtualEntitySingleIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/id/VirtualEntitySingleIdMapper.java @@ -17,6 +17,7 @@ import org.hibernate.envers.internal.tools.ReflectionTools; import org.hibernate.property.access.spi.Getter; import org.hibernate.property.access.spi.Setter; import org.hibernate.proxy.HibernateProxy; +import org.hibernate.proxy.LazyInitializer; import org.hibernate.service.ServiceRegistry; import org.hibernate.type.EntityType; @@ -133,9 +134,9 @@ public class VirtualEntitySingleIdMapper extends SingleIdMapper { data.put( propertyData.getName(), null ); } else { - if ( isHibernateProxy( obj ) ) { - final HibernateProxy proxy = asHibernateProxy( obj ); - data.put( propertyData.getName(), proxy.getHibernateLazyInitializer().getInternalIdentifier() ); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( obj ); + if ( lazyInitializer != null ) { + data.put( propertyData.getName(), lazyInitializer.getInternalIdentifier() ); } else { final Object value = getValueFromObject( propertyData, obj ); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/tools/EntityTools.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/tools/EntityTools.java index 9beb80c1ba..b25d55e722 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/tools/EntityTools.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/tools/EntityTools.java @@ -14,6 +14,7 @@ import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.proxy.HibernateProxy; +import org.hibernate.proxy.LazyInitializer; import static org.hibernate.engine.internal.ManagedTypeHelper.asHibernateProxy; import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy; @@ -34,27 +35,27 @@ public abstract class EntityTools { return null; } - if ( isHibernateProxy( obj ) ) { - final HibernateProxy hibernateProxy = asHibernateProxy( obj ); - return hibernateProxy.getHibernateLazyInitializer().getInternalIdentifier(); + final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( obj ); + if ( lazyInitializer != null ) { + return lazyInitializer.getInternalIdentifier(); } return session.getEntityPersister( entityName, obj ).getIdentifier( obj, session ); } - public static Object getTargetFromProxy(SessionFactoryImplementor sessionFactoryImplementor, HibernateProxy proxy) { - if ( !proxy.getHibernateLazyInitializer().isUninitialized() || activeProxySession( proxy ) ) { - return proxy.getHibernateLazyInitializer().getImplementation(); + public static Object getTargetFromProxy(final SessionFactoryImplementor sessionFactoryImplementor, final LazyInitializer lazyInitializer) { + if ( !lazyInitializer.isUninitialized() || activeProxySession( lazyInitializer ) ) { + return lazyInitializer.getImplementation(); } - final SharedSessionContractImplementor sessionImplementor = proxy.getHibernateLazyInitializer().getSession(); + final SharedSessionContractImplementor sessionImplementor = lazyInitializer.getSession(); final Session tempSession = sessionImplementor == null ? sessionFactoryImplementor.openTemporarySession() : sessionImplementor.getFactory().openTemporarySession(); try { return tempSession.get( - proxy.getHibernateLazyInitializer().getEntityName(), - proxy.getHibernateLazyInitializer().getInternalIdentifier() + lazyInitializer.getEntityName(), + lazyInitializer.getInternalIdentifier() ); } finally { @@ -62,8 +63,8 @@ public abstract class EntityTools { } } - private static boolean activeProxySession(HibernateProxy proxy) { - final Session session = (Session) proxy.getHibernateLazyInitializer().getSession(); + private static boolean activeProxySession(final LazyInitializer lazyInitializer) { + final Session session = (Session) lazyInitializer.getSession(); return session != null && session.isOpen() && session.isConnected(); }