finally fix the warnings and typecasts in DefaultMergeEventListener

by using MergeContext instead of (raw!) Map
This commit is contained in:
Gavin King 2022-01-30 13:15:37 +01:00
parent b75613cff1
commit 7570d15291
11 changed files with 42 additions and 43 deletions

View File

@ -17,6 +17,7 @@ import org.hibernate.ReplicationMode;
import org.hibernate.TransientPropertyValueException;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.event.spi.MergeContext;
import org.hibernate.event.spi.EventSource;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.persister.entity.EntityPersister;
@ -258,7 +259,7 @@ public class CascadingActions {
boolean isCascadeDeleteEnabled)
throws HibernateException {
LOG.tracev( "Cascading to merge: {0}", entityName );
session.merge( entityName, child, (Map<?,?>) anything );
session.merge( entityName, child, (MergeContext) anything );
}
@Override

View File

@ -37,6 +37,7 @@ import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.event.spi.MergeContext;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.jdbc.ReturningWork;
import org.hibernate.jdbc.Work;
@ -1096,7 +1097,7 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
}
@Override
public void merge(String entityName, Object object, Map copiedAlready) throws HibernateException {
public void merge(String entityName, Object object, MergeContext copiedAlready) throws HibernateException {
delegate.merge( entityName, object, copiedAlready );
}

View File

@ -11,6 +11,7 @@ import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.event.spi.MergeContext;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.resource.transaction.spi.TransactionCoordinator;
@ -77,7 +78,7 @@ public interface SessionImplementor extends Session, SharedSessionContractImplem
* @deprecated OperationalContext should cover this overload I believe
*/
@Deprecated
void merge(String entityName, Object object, Map copiedAlready) throws HibernateException;
void merge(String entityName, Object object, MergeContext copiedAlready) throws HibernateException;
/**
* @deprecated OperationalContext should cover this overload I believe

View File

@ -272,7 +272,7 @@ public abstract class AbstractSaveEventListener
}
if ( substitute ) {
persister.setPropertyValues( entity, values );
persister.setValues( entity, values );
}
TypeHelper.deepCopy(
@ -355,7 +355,7 @@ public abstract class AbstractSaveEventListener
}
}
protected Map getMergeMap(Object anything) {
protected Map<Object,Object> getMergeMap(Object anything) {
return null;
}
@ -377,7 +377,7 @@ public abstract class AbstractSaveEventListener
Type[] types,
EventSource source) {
WrapVisitor visitor = new WrapVisitor( entity, id, source );
// substitutes into values by side-effect
// substitutes into values by side effect
visitor.processEntityPropertyValues( values, types );
return visitor.isSubstitutionRequired();
}
@ -454,7 +454,7 @@ public abstract class AbstractSaveEventListener
}
/**
* Handles to calls needed to perform post-save cascades.
* Handles calls needed to perform post-save cascades.
*
* @param source The session from which the event originated.
* @param persister The entity's persister instance.

View File

@ -29,6 +29,7 @@ import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.event.spi.EntityCopyObserver;
import org.hibernate.event.spi.EntityCopyObserverFactory;
import org.hibernate.event.spi.EventSource;
import org.hibernate.event.spi.MergeContext;
import org.hibernate.event.spi.MergeEvent;
import org.hibernate.event.spi.MergeEventListener;
import org.hibernate.internal.CoreLogging;
@ -37,7 +38,6 @@ import org.hibernate.loader.ast.spi.CascadingFetchProfile;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.TypeHelper;
@ -52,7 +52,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( DefaultMergeEventListener.class );
@Override
protected Map getMergeMap(Object anything) {
protected Map<Object,Object> getMergeMap(Object anything) {
return ( (MergeContext) anything ).invertMap();
}
@ -87,9 +87,8 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
* @param event The merge event to be handled.
*
*/
public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
public void onMerge(MergeEvent event, MergeContext copiedAlready) throws HibernateException {
final MergeContext copyCache = (MergeContext) copiedAlready;
final EventSource source = event.getSession();
final Object original = event.getOriginal();
@ -127,14 +126,14 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
entity = original;
}
if ( copyCache.containsKey( entity ) && copyCache.isOperatedOn( entity ) ) {
if ( ((MergeContext) copiedAlready).containsKey( entity ) && ((MergeContext) copiedAlready).isOperatedOn( entity ) ) {
LOG.trace( "Already in merge process" );
event.setResult( entity );
}
else {
if ( copyCache.containsKey( entity ) ) {
if ( ((MergeContext) copiedAlready).containsKey( entity ) ) {
LOG.trace( "Already in copyCache; setting in merge process" );
copyCache.setOperatedOn( entity, true );
((MergeContext) copiedAlready).setOperatedOn( entity, true );
}
event.setEntity( entity );
EntityState entityState = null;
@ -167,13 +166,13 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
switch ( entityState ) {
case DETACHED:
entityIsDetached( event, copyCache );
entityIsDetached( event, (MergeContext) copiedAlready);
break;
case TRANSIENT:
entityIsTransient( event, copyCache );
entityIsTransient( event, (MergeContext) copiedAlready);
break;
case PERSISTENT:
entityIsPersistent( event, copyCache );
entityIsPersistent( event, (MergeContext) copiedAlready);
break;
default: //DELETED
throw new ObjectDeletedException(
@ -188,7 +187,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
}
protected void entityIsPersistent(MergeEvent event, Map copyCache) {
protected void entityIsPersistent(MergeEvent event, MergeContext copyCache) {
LOG.trace( "Ignoring persistent instance" );
//TODO: check that entry.getIdentifier().equals(requestedId)
@ -197,7 +196,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
final EventSource source = event.getSession();
final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
( (MergeContext) copyCache ).put( entity, entity, true ); //before cascade!
copyCache.put( entity, entity, true ); //before cascade!
cascadeOnMerge( source, persister, entity, copyCache );
copyValues( persister, entity, entity, source, copyCache );
@ -205,7 +204,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
event.setResult( entity );
}
protected void entityIsTransient(MergeEvent event, Map copyCache) {
protected void entityIsTransient(MergeEvent event, MergeContext copyCache) {
LOG.trace( "Merging transient instance" );
@ -229,7 +228,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
copy = session.instantiate( persister, id );
//before cascade!
( (MergeContext) copyCache ).put( entity, copy, true );
copyCache.put( entity, copy, true );
}
// cascade first, so that all unsaved objects get their
@ -261,7 +260,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
String entityName,
Object requestedId,
EventSource source,
Map copyCache) {
MergeContext copyCache) {
//this bit is only *really* absolutely necessary for handling
//requestedId, but is also good if we merge multiple object
//graphs, since it helps ensure uniqueness
@ -273,7 +272,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
}
}
protected void entityIsDetached(MergeEvent event, Map copyCache) {
protected void entityIsDetached(MergeEvent event, MergeContext copyCache) {
LOG.trace( "Merging detached instance" );
@ -317,7 +316,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
}
else {
// before cascade!
( (MergeContext) copyCache ).put( entity, result, true );
copyCache.put( entity, result, true );
final Object target = unproxyManagedForDetachedMerging( entity, result, persister, source );
@ -345,7 +344,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
copyValues( persister, entity, target, source, copyCache );
//copyValues works by reflection, so explicitly mark the entity instance dirty
markInterceptorDirty( entity, target, persister );
markInterceptorDirty( entity, target );
event.setResult( result );
}
@ -390,7 +389,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
return managed;
}
private void markInterceptorDirty(final Object entity, final Object target, EntityPersister persister) {
private void markInterceptorDirty(final Object entity, final Object target) {
// for enhanced entities, copy over the dirty attributes
if ( entity instanceof SelfDirtinessTracker && target instanceof SelfDirtinessTracker ) {
// clear, because setting the embedded attributes dirties them
@ -446,7 +445,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
final Object entity,
final Object target,
final SessionImplementor source,
final Map copyCache) {
final MergeContext copyCache) {
final Object[] copiedValues = TypeHelper.replace(
persister.getValues( entity ),
persister.getValues( target ),
@ -456,7 +455,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
copyCache
);
persister.setPropertyValues( target, copiedValues );
persister.setValues( target, copiedValues );
}
protected void copyValues(
@ -464,7 +463,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
final Object entity,
final Object target,
final SessionImplementor source,
final Map copyCache,
final MergeContext copyCache,
final ForeignKeyDirection foreignKeyDirection) {
final Object[] copiedValues;
@ -495,7 +494,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
);
}
persister.setPropertyValues( target, copiedValues );
persister.setValues( target, copiedValues );
}
/**
@ -510,7 +509,7 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener impleme
final EventSource source,
final EntityPersister persister,
final Object entity,
final Map copyCache
final MergeContext copyCache
) {
final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
persistenceContext.incrementCascadeLevel();

View File

@ -9,7 +9,7 @@ package org.hibernate.event.spi;
/**
* An observer for detection of multiple entity representations for a persistent entity being merged.
*
* @see org.hibernate.event.internal.MergeContext
* @see MergeContext
*
* @author Gail Badner
*/

View File

@ -39,7 +39,7 @@ public interface EventSource extends SessionImplementor {
/**
* Cascade merge an entity instance
*/
void merge(String entityName, Object object, Map copiedAlready) throws HibernateException;
void merge(String entityName, Object object, MergeContext copiedAlready) throws HibernateException;
/**
* Cascade persist an entity instance
*/

View File

@ -4,7 +4,7 @@
* 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;
package org.hibernate.event.spi;
import java.util.Collection;
import java.util.Collections;
@ -12,8 +12,6 @@ import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import org.hibernate.event.spi.EntityCopyObserver;
import org.hibernate.event.spi.EventSource;
import org.hibernate.pretty.MessageHelper;
/**

View File

@ -6,8 +6,6 @@
*/
package org.hibernate.event.spi;
import java.util.Map;
import org.hibernate.HibernateException;
/**
@ -29,6 +27,6 @@ public interface MergeEventListener {
*
* @param event The merge event to be handled.
*/
void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException;
void onMerge(MergeEvent event, MergeContext copiedAlready) throws HibernateException;
}

View File

@ -69,6 +69,7 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.Status;
import org.hibernate.engine.transaction.spi.TransactionImplementor;
import org.hibernate.engine.transaction.spi.TransactionObserver;
import org.hibernate.event.spi.MergeContext;
import org.hibernate.event.spi.AutoFlushEvent;
import org.hibernate.event.spi.AutoFlushEventListener;
import org.hibernate.event.spi.ClearEvent;
@ -796,7 +797,7 @@ public class SessionImpl
}
@Override
public void merge(String entityName, Object object, Map copiedAlready) throws HibernateException {
public void merge(String entityName, Object object, MergeContext copiedAlready) throws HibernateException {
checkOpenOrWaitingForAutoClose();
fireMerge( copiedAlready, new MergeEvent( entityName, object, this ) );
}
@ -822,10 +823,10 @@ public class SessionImpl
return event.getResult();
}
private void fireMerge(final Map copiedAlready, final MergeEvent event) {
private void fireMerge(final MergeContext mergeContext, final MergeEvent event) {
try {
pulseTransactionCoordinator();
fastSessionServices.eventListenerGroup_MERGE.fireEventOnEachListener( event, copiedAlready, MergeEventListener::onMerge );
fastSessionServices.eventListenerGroup_MERGE.fireEventOnEachListener( event, mergeContext, MergeEventListener::onMerge );
}
catch ( ObjectDeletedException sse ) {
throw getExceptionConverter().convert( new IllegalArgumentException( sse ) );

View File

@ -18,7 +18,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.event.internal.MergeContext;
import org.hibernate.event.spi.MergeContext;
import org.hibernate.event.spi.EntityCopyObserver;
import org.hibernate.event.spi.EventSource;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;