clean up the internal DefaultVisitor hierarchy

by removing two obsolete abstract superclasses

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-09-02 10:41:26 +02:00
parent 9c75adcffc
commit 763401a5c8
7 changed files with 76 additions and 139 deletions

View File

@ -1,33 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.event.internal;
import org.hibernate.LockOptions;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.event.spi.EventSource;
import org.hibernate.loader.ast.internal.LoaderHelper;
/**
* A convenience base class for listeners that respond to requests to perform a
* pessimistic lock upgrade on an entity.
*
* @author Gavin King
*/
public abstract class AbstractLockUpgradeEventListener extends AbstractReassociateEventListener {
/**
* Performs a pessimistic lock upgrade on a given entity, if needed.
*
* @param object The entity for which to upgrade the lock.
* @param entry The entity's EntityEntry instance.
* @param lockOptions contains the requested lock mode.
* @param source The session which is the source of the event being processed.
*/
protected void upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource source) {
LoaderHelper.upgradeLock( object, entry, lockOptions, source );
}
}

View File

@ -1,90 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.event.internal;
import org.hibernate.LockMode;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.Status;
import org.hibernate.event.spi.AbstractEvent;
import org.hibernate.event.spi.EventSource;
import org.hibernate.internal.CoreLogging;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.type.TypeHelper;
import org.jboss.logging.Logger;
import static org.hibernate.engine.internal.Versioning.getVersion;
/**
* A convenience base class for listeners that respond to requests to reassociate an entity
* to a session ( such as through lock() or update() ).
*
* @author Gavin King
*/
public abstract class AbstractReassociateEventListener {
private static final Logger log = CoreLogging.logger( AbstractReassociateEventListener.class );
/**
* Associates a given entity (either transient or associated with another session) to
* the given session.
*
* @param event The event triggering the re-association
* @param object The entity to be associated
* @param id The id of the entity.
* @param persister The entity's persister instance.
*
* @return An EntityEntry representing the entity within this session.
*/
protected final EntityEntry reassociate(AbstractEvent event, Object object, Object id, EntityPersister persister) {
if ( log.isTraceEnabled() ) {
log.tracev(
"Reassociating transient instance: {0}",
MessageHelper.infoString( persister, id, event.getFactory() )
);
}
final EventSource source = event.getSession();
final EntityKey key = source.generateEntityKey( id, persister );
final PersistenceContext persistenceContext = source.getPersistenceContext();
persistenceContext.checkUniqueness( key, object );
//get a snapshot
Object[] values = persister.getValues( object );
TypeHelper.deepCopy(
values,
persister.getPropertyTypes(),
persister.getPropertyUpdateability(),
values,
source
);
Object version = getVersion( values, persister );
EntityEntry newEntry = persistenceContext.addEntity(
object,
persister.isMutable() ? Status.MANAGED : Status.READ_ONLY,
values,
key,
version,
LockMode.NONE,
true,
persister,
false
);
new OnLockVisitor( source, id, object ).process( object, persister );
persister.afterReassociate( object, source );
return newEntry;
}
}

View File

@ -50,9 +50,7 @@ import static org.hibernate.pretty.MessageHelper.infoString;
* *
* @author Steve Ebersole. * @author Steve Ebersole.
*/ */
public abstract class AbstractSaveEventListener<C> public abstract class AbstractSaveEventListener<C> implements CallbackRegistryConsumer {
extends AbstractReassociateEventListener
implements CallbackRegistryConsumer {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( AbstractSaveEventListener.class ); private static final CoreMessageLogger LOG = CoreLogging.messageLogger( AbstractSaveEventListener.class );
private CallbackRegistry callbackRegistry; private CallbackRegistry callbackRegistry;

View File

@ -14,25 +14,33 @@ import org.hibernate.engine.internal.CascadePoint;
import org.hibernate.engine.internal.ForeignKeys; import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.spi.CascadingActions; import org.hibernate.engine.spi.CascadingActions;
import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.Status;
import org.hibernate.event.spi.AbstractEvent;
import org.hibernate.event.spi.EventSource; import org.hibernate.event.spi.EventSource;
import org.hibernate.event.spi.LockEvent; import org.hibernate.event.spi.LockEvent;
import org.hibernate.event.spi.LockEventListener; import org.hibernate.event.spi.LockEventListener;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.TypeHelper;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import static org.hibernate.engine.internal.Versioning.getVersion;
import static org.hibernate.loader.ast.internal.LoaderHelper.upgradeLock;
import static org.hibernate.pretty.MessageHelper.infoString;
/** /**
* Defines the default lock event listeners used by hibernate to lock entities * Defines the default lock event listeners used by hibernate to lock entities
* in response to generated lock events. * in response to generated lock events.
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class DefaultLockEventListener extends AbstractLockUpgradeEventListener implements LockEventListener { public class DefaultLockEventListener implements LockEventListener {
private static final CoreMessageLogger LOG = Logger.getMessageLogger( private static final CoreMessageLogger LOG = Logger.getMessageLogger(
MethodHandles.lookup(), MethodHandles.lookup(),
@ -100,4 +108,58 @@ public class DefaultLockEventListener extends AbstractLockUpgradeEventListener i
} }
} }
/**
* Associates a given entity (either transient or associated with another session)
* to the given session.
*
* @param event The event triggering the re-association
* @param object The entity to be associated
* @param id The id of the entity.
* @param persister The entity's persister instance.
*
* @return An EntityEntry representing the entity within this session.
*/
protected final EntityEntry reassociate(AbstractEvent event, Object object, Object id, EntityPersister persister) {
if ( LOG.isTraceEnabled() ) {
LOG.tracev(
"Reassociating transient instance: {0}",
infoString( persister, id, event.getFactory() )
);
}
final EventSource source = event.getSession();
final EntityKey key = source.generateEntityKey( id, persister );
final PersistenceContext persistenceContext = source.getPersistenceContext();
persistenceContext.checkUniqueness( key, object );
//get a snapshot
final Object[] values = persister.getValues( object );
TypeHelper.deepCopy(
values,
persister.getPropertyTypes(),
persister.getPropertyUpdateability(),
values,
source
);
final EntityEntry newEntry = persistenceContext.addEntity(
object,
persister.isMutable() ? Status.MANAGED : Status.READ_ONLY,
values,
key,
getVersion( values, persister ),
LockMode.NONE,
true,
persister,
false
);
new OnLockVisitor( source, id, object ).process( object, persister );
persister.afterReassociate( object, source );
return newEntry;
}
} }

View File

@ -124,9 +124,7 @@ public class DefaultMergeEventListener
else if ( isPersistentAttributeInterceptable( original ) ) { else if ( isPersistentAttributeInterceptable( original ) ) {
final PersistentAttributeInterceptor interceptor = final PersistentAttributeInterceptor interceptor =
asPersistentAttributeInterceptable( original ).$$_hibernate_getInterceptor(); asPersistentAttributeInterceptable( original ).$$_hibernate_getInterceptor();
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) { if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor proxyInterceptor ) {
final EnhancementAsProxyLazinessInterceptor proxyInterceptor =
(EnhancementAsProxyLazinessInterceptor) interceptor;
LOG.trace( "Ignoring uninitialized enhanced-proxy" ); LOG.trace( "Ignoring uninitialized enhanced-proxy" );
event.setResult( source.load( proxyInterceptor.getEntityName(), proxyInterceptor.getIdentifier() ) ); event.setResult( source.load( proxyInterceptor.getEntityName(), proxyInterceptor.getIdentifier() ) );
} }
@ -350,15 +348,14 @@ public class DefaultMergeEventListener
} }
@Override @Override
protected Object processCollection(Object collection, CollectionType collectionType) { protected Object processCollection(Object collection, CollectionType collectionType) {
if ( collection instanceof PersistentCollection ) { if ( collection instanceof PersistentCollection<?> persistentCollection ) {
final PersistentCollection<?> coll = (PersistentCollection<?>) collection;
final CollectionPersister persister = final CollectionPersister persister =
getSession().getFactory().getMappingMetamodel() getSession().getFactory().getMappingMetamodel()
.getCollectionDescriptor( collectionType.getRole() ); .getCollectionDescriptor( collectionType.getRole() );
final CollectionEntry collectionEntry = final CollectionEntry collectionEntry =
getSession().getPersistenceContextInternal().getCollectionEntry( coll ); getSession().getPersistenceContextInternal().getCollectionEntry( persistentCollection );
if ( !coll.equalsSnapshot( persister ) ) { if ( !persistentCollection.equalsSnapshot( persister ) ) {
collectionEntry.resetStoredSnapshot( coll, coll.getSnapshot( persister ) ); collectionEntry.resetStoredSnapshot( persistentCollection, persistentCollection.getSnapshot( persister ) );
} }
} }
return null; return null;

View File

@ -27,9 +27,7 @@ import static org.hibernate.pretty.MessageHelper.infoString;
* @author Eric Dalquist * @author Eric Dalquist
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class DefaultResolveNaturalIdEventListener public class DefaultResolveNaturalIdEventListener implements ResolveNaturalIdEventListener {
extends AbstractLockUpgradeEventListener
implements ResolveNaturalIdEventListener {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( DefaultResolveNaturalIdEventListener.class ); private static final CoreMessageLogger LOG = CoreLogging.messageLogger( DefaultResolveNaturalIdEventListener.class );

View File

@ -46,7 +46,12 @@ public class LoaderHelper {
/** /**
* Ensure the {@linkplain LockMode} associated with the entity in relation to a * Ensure the {@linkplain LockMode} associated with the entity in relation to a
* persistence context is {@linkplain LockMode#greaterThan great or equal} to the * persistence context is {@linkplain LockMode#greaterThan great or equal} to the
* requested mode. * requested mode, performing a pessimistic lock upgrade on a given entity, if needed.
*
* @param object The entity for which to upgrade the lock.
* @param entry The entity's {@link EntityEntry} instance.
* @param lockOptions Contains the requested lock mode.
* @param session The session which is the source of the event being processed.
*/ */
public static void upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource session) { public static void upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource session) {
final LockMode requestedLockMode = lockOptions.getLockMode(); final LockMode requestedLockMode = lockOptions.getLockMode();