Skip to content

Commit

Permalink
Adapt Hibernate Reactive to HHH-15616
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne authored and DavideD committed Nov 8, 2022
1 parent b48b5f6 commit 273d824
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import org.hibernate.HibernateException;
import org.hibernate.TransientObjectException;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.engine.internal.ManagedTypeHelper;
import org.hibernate.engine.internal.NonNullableTransientDependencies;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SelfDirtinessTracker;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.StringHelper;
Expand Down Expand Up @@ -181,8 +181,8 @@ private void trackDirt(Object value, String propertyName, Object returnedValue)
// When bytecode-enhancement is used for dirty-checking, the change should
// only be tracked when returnedValue was nullified (1)).
if ( value != returnedValue && returnedValue == null
&& self instanceof SelfDirtinessTracker ) {
( (SelfDirtinessTracker) self ).$$_hibernate_trackChange( propertyName );
&& ManagedTypeHelper.isSelfDirtinessTracker( self ) ) {
ManagedTypeHelper.asSelfDirtinessTracker( self ).$$_hibernate_trackChange( propertyName );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.NonUniqueObjectException;
import org.hibernate.action.internal.AbstractEntityInsertAction;
import org.hibernate.engine.internal.CascadePoint;
import org.hibernate.engine.internal.ManagedTypeHelper;
import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityEntryExtraState;
Expand Down Expand Up @@ -115,9 +116,7 @@ protected CompletionStage<Void> reactiveSaveWithGeneratedId(
boolean requiresImmediateIdAccess) {
callbackRegistry.preCreate( entity );

if ( entity instanceof SelfDirtinessTracker ) {
( (SelfDirtinessTracker) entity ).$$_hibernate_clearDirtyAttributes();
}
ManagedTypeHelper.processIfSelfDirtinessTracker( entity, SelfDirtinessTracker::$$_hibernate_clearDirtyAttributes );

EntityPersister persister = source.getEntityPersister( entityName, entity );
boolean autoincrement = persister.isIdentifierAssignedByInsert();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.hibernate.*;
import org.hibernate.action.internal.DelayedPostInsertIdentifier;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.engine.internal.ManagedTypeHelper;
import org.hibernate.engine.internal.Nullability;
import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.*;
Expand Down Expand Up @@ -225,13 +226,12 @@ private boolean isUpdateNecessary(final FlushEntityEvent event, final boolean mi
return true;
}
else {
if ( event.getEntity() instanceof SelfDirtinessTracker ) {
( (SelfDirtinessTracker) event.getEntity() ).$$_hibernate_clearDirtyAttributes();
}
final Object entity = event.getEntity();
ManagedTypeHelper.processIfSelfDirtinessTracker( entity, SelfDirtinessTracker::$$_hibernate_clearDirtyAttributes );
event.getSession()
.getFactory()
.getCustomEntityDirtinessStrategy()
.resetDirty( event.getEntity(), event.getEntityEntry().getPersister(), event.getSession() );
.resetDirty( entity, event.getEntityEntry().getPersister(), event.getSession() );
return false;
}
}
Expand Down Expand Up @@ -504,12 +504,13 @@ protected void dirtyCheck(final FlushEntityEvent event) throws HibernateExceptio
);

if ( dirtyProperties == null ) {
if ( entity instanceof SelfDirtinessTracker ) {
if ( ( (SelfDirtinessTracker) entity ).$$_hibernate_hasDirtyAttributes() || persister.hasMutableProperties() ) {
if ( ManagedTypeHelper.isSelfDirtinessTracker( entity ) ) {
final SelfDirtinessTracker tracker = ManagedTypeHelper.asSelfDirtinessTracker( entity );
if ( tracker.$$_hibernate_hasDirtyAttributes() || persister.hasMutableProperties() ) {
dirtyProperties = persister.resolveDirtyAttributeIndexes(
values,
loadedState,
( (SelfDirtinessTracker) entity ).$$_hibernate_getDirtyAttributes(),
tracker.$$_hibernate_getDirtyAttributes(),
session
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hibernate.WrongClassException;
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.engine.internal.CascadePoint;
import org.hibernate.engine.internal.ManagedTypeHelper;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
Expand Down Expand Up @@ -49,6 +50,10 @@
import org.hibernate.type.ForeignKeyDirection;
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.isPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.ManagedTypeHelper.isSelfDirtinessTracker;
import static org.hibernate.event.internal.EntityState.DETACHED;
import static org.hibernate.reactive.util.impl.CompletionStages.loop;
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
Expand Down Expand Up @@ -128,8 +133,8 @@ public CompletionStage<Void> reactiveOnMerge(MergeEvent event, MergeContext copy
entity = li.getImplementation();
}
}
else if ( original instanceof PersistentAttributeInterceptable ) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) original;
else if ( isPersistentAttributeInterceptable( original ) ) {
final PersistentAttributeInterceptable interceptable = ManagedTypeHelper.asPersistentAttributeInterceptable( original );
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
final EnhancementAsProxyLazinessInterceptor proxyInterceptor = (EnhancementAsProxyLazinessInterceptor) interceptor;
Expand Down Expand Up @@ -260,8 +265,8 @@ protected CompletionStage<Void> entityIsTransient(MergeEvent event, MergeContext
.thenAccept( v -> {
event.setResult(copy);

if (copy instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) copy;
if ( isPersistentAttributeInterceptable( copy ) ) {
final PersistentAttributeInterceptable interceptable = asPersistentAttributeInterceptable( copy );
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if (interceptor == null) {
persister.getBytecodeEnhancementMetadata().injectInterceptor( copy, id, session );
Expand Down Expand Up @@ -377,11 +382,11 @@ private Object unproxyManagedForDetachedMerging(
return source.getPersistenceContextInternal().unproxy( managed );
}

if ( incoming instanceof PersistentAttributeInterceptable
if ( isPersistentAttributeInterceptable( incoming )
&& persister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ) {

final PersistentAttributeInterceptor incomingInterceptor = ( (PersistentAttributeInterceptable) incoming ).$$_hibernate_getInterceptor();
final PersistentAttributeInterceptor managedInterceptor = ( (PersistentAttributeInterceptable) managed ).$$_hibernate_getInterceptor();
final PersistentAttributeInterceptor incomingInterceptor = asPersistentAttributeInterceptable( incoming ).$$_hibernate_getInterceptor();
final PersistentAttributeInterceptor managedInterceptor = asPersistentAttributeInterceptable( managed ).$$_hibernate_getInterceptor();

// todo - do we need to specially handle the case where both `incoming` and `managed` are initialized, but
// with different attributes initialized?
Expand All @@ -407,10 +412,10 @@ private Object unproxyManagedForDetachedMerging(

private void markInterceptorDirty(final Object entity, final Object target, EntityPersister persister) {
// for enhanced entities, copy over the dirty attributes
if ( entity instanceof SelfDirtinessTracker && target instanceof SelfDirtinessTracker ) {
if ( isSelfDirtinessTracker( entity ) && isSelfDirtinessTracker( target ) ) {
// clear, because setting the embedded attributes dirties them
SelfDirtinessTracker entityTracker = (SelfDirtinessTracker) entity;
SelfDirtinessTracker targetTracker = (SelfDirtinessTracker) target;
SelfDirtinessTracker entityTracker = asSelfDirtinessTracker( entity );
SelfDirtinessTracker targetTracker = asSelfDirtinessTracker( target );
targetTracker.$$_hibernate_clearDirtyAttributes();
for ( String fieldName : entityTracker.$$_hibernate_getDirtyAttributes() ) {
targetTracker.$$_hibernate_trackChange( fieldName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.internal.ManagedTypeHelper;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
Expand Down Expand Up @@ -2076,8 +2077,8 @@ else if ( association instanceof PersistentCollection) {
//this unfortunately doesn't work for stateless session because the session ref gets set to null
session = ( (AbstractPersistentCollection) association ).getSession();
}
else if ( association instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) association;
else if ( ManagedTypeHelper.isPersistentAttributeInterceptable( association ) ) {
final PersistentAttributeInterceptable interceptable = ManagedTypeHelper.asPersistentAttributeInterceptable( association );
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
session = ( (EnhancementAsProxyLazinessInterceptor) interceptor ).getLinkedSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
import org.hibernate.dialect.CockroachDB192Dialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.OptimisticLockStyle;
import org.hibernate.engine.internal.ManagedTypeHelper;
import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
Expand Down Expand Up @@ -1206,8 +1206,7 @@ default CompletionStage<Object> reactiveInitializeLazyPropertiesFromDatastore(
throw new AssertionFailure( "no lazy properties" );
}

final PersistentAttributeInterceptor interceptor =
( (PersistentAttributeInterceptable) entity ).$$_hibernate_getInterceptor();
final PersistentAttributeInterceptor interceptor = ManagedTypeHelper.asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
if ( interceptor == null ) {
throw new AssertionFailure( "Expecting bytecode interceptor to be non-null" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
import org.hibernate.reactive.util.impl.CompletionStages;


import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
import static org.hibernate.engine.spi.PersistenceContext.NaturalIdHelper.INVALID_NATURAL_ID_REFERENCE;
import static org.hibernate.reactive.common.InternalStateAssertions.assertUseOnEventLoop;
import static org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister.forceInitialize;
Expand Down Expand Up @@ -276,8 +278,8 @@ else if ( association instanceof PersistentCollection ) {
.thenApply( v -> association );
}
}
else if ( association instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) association;
else if ( isPersistentAttributeInterceptable( association ) ) {
final PersistentAttributeInterceptable interceptable = asPersistentAttributeInterceptable( association );
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
EnhancementAsProxyLazinessInterceptor eapli = (EnhancementAsProxyLazinessInterceptor) interceptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
import static org.hibernate.reactive.id.impl.IdentifierGeneration.assignIdIfNecessary;
import static org.hibernate.reactive.id.impl.IdentifierGeneration.generateId;
import static org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister.forceInitialize;
Expand Down Expand Up @@ -803,8 +805,8 @@ else if ( association instanceof PersistentCollection ) {
.thenApply( v -> association );
}
}
else if ( association instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) association;
else if ( isPersistentAttributeInterceptable( association ) ) {
final PersistentAttributeInterceptable interceptable = asPersistentAttributeInterceptable( association );
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
EnhancementAsProxyLazinessInterceptor eapli = (EnhancementAsProxyLazinessInterceptor) interceptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.internal.ManagedTypeHelper;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
Expand Down Expand Up @@ -2070,8 +2071,8 @@ static <T> CompletionStage<T> fetch(T association) {
else if ( association instanceof PersistentCollection) {
session = ( (AbstractPersistentCollection) association ).getSession();
}
else if ( association instanceof PersistentAttributeInterceptable) {
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) association;
else if ( ManagedTypeHelper.isPersistentAttributeInterceptable( association ) ) {
final PersistentAttributeInterceptable interceptable = ManagedTypeHelper.asPersistentAttributeInterceptable( association );
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor) {
session = ( (EnhancementAsProxyLazinessInterceptor) interceptor ).getLinkedSession();
Expand Down

0 comments on commit 273d824

Please sign in to comment.