HHH-15790 Refactor casting strategies for HibernateProxy across the codebase
This commit is contained in:
parent
afc35f6c62
commit
33ce6a3d79
|
@ -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;
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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" );
|
||||
|
|
|
@ -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) ) {
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -213,11 +213,10 @@ public class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T>, 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 {
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue