Skip to content

Commit 9c75adc

Browse files
committed
cleanups in event listeners
Signed-off-by: Gavin King <[email protected]>
1 parent ec3be76 commit 9c75adc

File tree

3 files changed

+45
-63
lines changed

3 files changed

+45
-63
lines changed

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.hibernate.jpa.event.spi.CallbackRegistry;
3939
import org.hibernate.jpa.event.spi.CallbackRegistryConsumer;
4040
import org.hibernate.jpa.event.spi.CallbackType;
41+
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
4142
import org.hibernate.persister.collection.CollectionPersister;
4243
import org.hibernate.persister.entity.EntityPersister;
4344
import org.hibernate.pretty.MessageHelper;
@@ -463,12 +464,12 @@ private Object[] createDeletedState(
463464

464465
final String[] propertyNames = persister.getPropertyNames();
465466
final BytecodeEnhancementMetadata enhancementMetadata = persister.getBytecodeEnhancementMetadata();
467+
final MappingMetamodelImplementor metamodel = persister.getFactory().getMappingMetamodel();
466468
for ( int i = 0; i < types.length; i++) {
467469
if ( types[i] instanceof CollectionType collectionType
468470
&& !enhancementMetadata.isAttributeLoaded( parent, propertyNames[i] ) ) {
469471
final CollectionPersister collectionDescriptor =
470-
persister.getFactory().getMappingMetamodel()
471-
.getCollectionDescriptor( collectionType.getRole() );
472+
metamodel.getCollectionDescriptor( collectionType.getRole() );
472473
if ( collectionDescriptor.needsRemove() || collectionDescriptor.hasCache() ) {
473474
final Object keyOfOwner = collectionType.getKeyOfOwner( parent, eventSource.getSession() );
474475
// This will make sure that a CollectionEntry exists

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -247,31 +247,26 @@ private static Object copyCompositeTypeId(
247247
CompositeType compositeType,
248248
EventSource session,
249249
MergeContext mergeContext) {
250-
final SessionFactoryImplementor sessionFactory = session.getSessionFactory();
251-
final Object idCopy = compositeType.deepCopy( id, sessionFactory );
250+
final SessionFactoryImplementor factory = session.getSessionFactory();
251+
final Object idCopy = compositeType.deepCopy( id, factory );
252252
final Type[] subtypes = compositeType.getSubtypes();
253253
final Object[] propertyValues = compositeType.getPropertyValues( id );
254254
final Object[] copyValues = compositeType.getPropertyValues( idCopy );
255255
for ( int i = 0; i < subtypes.length; i++ ) {
256256
final Type subtype = subtypes[i];
257257
if ( subtype instanceof EntityType ) {
258258
// the value of the copy in the MergeContext has the id assigned
259-
final Object o = mergeContext.get( propertyValues[i] );
260-
if ( o != null ) {
261-
copyValues[i] = o;
262-
}
263-
else {
264-
copyValues[i] = subtype.deepCopy( propertyValues[i], sessionFactory );
265-
}
259+
final Object object = mergeContext.get( propertyValues[i] );
260+
copyValues[i] = object == null ? subtype.deepCopy( propertyValues[i], factory ) : object;
266261
}
267-
else if ( subtype instanceof AnyType ) {
268-
copyValues[i] = copyCompositeTypeId( propertyValues[i], (AnyType) subtype, session, mergeContext );
262+
else if ( subtype instanceof AnyType anyType ) {
263+
copyValues[i] = copyCompositeTypeId( propertyValues[i], anyType, session, mergeContext );
269264
}
270-
else if ( subtype instanceof ComponentType ) {
271-
copyValues[i] = copyCompositeTypeId( propertyValues[i], (ComponentType) subtype, session, mergeContext );
265+
else if ( subtype instanceof ComponentType componentType ) {
266+
copyValues[i] = copyCompositeTypeId( propertyValues[i], componentType, session, mergeContext );
272267
}
273268
else {
274-
copyValues[i] = subtype.deepCopy( propertyValues[i], sessionFactory );
269+
copyValues[i] = subtype.deepCopy( propertyValues[i], factory );
275270
}
276271
}
277272
return compositeType.replacePropertyValues( idCopy, copyValues, session );
@@ -326,14 +321,16 @@ protected void entityIsTransient(MergeEvent event, Object id, MergeContext copyC
326321
event.setResult( copy );
327322

328323
if ( isPersistentAttributeInterceptable( copy ) ) {
329-
final PersistentAttributeInterceptor interceptor = asPersistentAttributeInterceptable( copy ).$$_hibernate_getInterceptor();
324+
final PersistentAttributeInterceptor interceptor =
325+
asPersistentAttributeInterceptable( copy ).$$_hibernate_getInterceptor();
330326
if ( interceptor == null ) {
331327
persister.getBytecodeEnhancementMetadata().injectInterceptor( copy, id, session );
332328
}
333329
}
334330
}
335331

336-
private static Object copyEntity(MergeContext copyCache, Object entity, EventSource session, EntityPersister persister, Object id) {
332+
private static Object copyEntity(
333+
MergeContext copyCache, Object entity, EventSource session, EntityPersister persister, Object id) {
337334
final Object existingCopy = copyCache.get( entity );
338335
if ( existingCopy != null ) {
339336
persister.setIdentifier( existingCopy, id, session );
@@ -352,23 +349,22 @@ private static class CollectionVisitor extends WrapVisitor {
352349
super( entity, id, session );
353350
}
354351
@Override
355-
protected Object processCollection(Object collection, CollectionType collectionType) throws HibernateException {
352+
protected Object processCollection(Object collection, CollectionType collectionType) {
356353
if ( collection instanceof PersistentCollection ) {
357354
final PersistentCollection<?> coll = (PersistentCollection<?>) collection;
358-
final CollectionPersister persister = getSession().getFactory()
359-
.getRuntimeMetamodels()
360-
.getMappingMetamodel()
361-
.getCollectionDescriptor( collectionType.getRole() );
362-
final CollectionEntry collectionEntry = getSession().getPersistenceContextInternal()
363-
.getCollectionEntry( coll );
355+
final CollectionPersister persister =
356+
getSession().getFactory().getMappingMetamodel()
357+
.getCollectionDescriptor( collectionType.getRole() );
358+
final CollectionEntry collectionEntry =
359+
getSession().getPersistenceContextInternal().getCollectionEntry( coll );
364360
if ( !coll.equalsSnapshot( persister ) ) {
365361
collectionEntry.resetStoredSnapshot( coll, coll.getSnapshot( persister ) );
366362
}
367363
}
368364
return null;
369365
}
370366
@Override
371-
Object processEntity(Object value, EntityType entityType) throws HibernateException {
367+
Object processEntity(Object value, EntityType entityType) {
372368
return null;
373369
}
374370
}
@@ -400,13 +396,9 @@ protected void entityIsDetached(MergeEvent event, Object copiedId, Object origin
400396
if ( originalId == null ) {
401397
originalId = persister.getIdentifier( entity, source );
402398
}
403-
final Object clonedIdentifier;
404-
if ( copiedId == null ) {
405-
clonedIdentifier = persister.getIdentifierType().deepCopy( originalId, event.getFactory() );
406-
}
407-
else {
408-
clonedIdentifier = copiedId;
409-
}
399+
final Object clonedIdentifier = copiedId == null
400+
? persister.getIdentifierType().deepCopy( originalId, event.getFactory() )
401+
: copiedId;
410402
final Object id = getDetachedEntityId( event, originalId, persister );
411403
// we must clone embedded composite identifiers, or we will get back the same instance that we pass in
412404
// apply the special MERGE fetch profile and perform the resolution (Session#get)
@@ -482,8 +474,7 @@ private static Object getDetachedEntityId(MergeEvent event, Object originalId, E
482474
}
483475
else {
484476
// check that entity id = requestedId
485-
final Object entityId = originalId;
486-
if ( !persister.getIdentifierType().isEqual( id, entityId, source.getFactory() ) ) {
477+
if ( !persister.getIdentifierType().isEqual( id, originalId, source.getFactory() ) ) {
487478
throw new HibernateException( "merge requested with id not matching id of passed entity" );
488479
}
489480
return id;
@@ -507,8 +498,8 @@ private static Object unproxyManagedForDetachedMerging(
507498
final PersistentAttributeInterceptor managedInterceptor =
508499
asPersistentAttributeInterceptable( managed ).$$_hibernate_getInterceptor();
509500

510-
// todo - do we need to specially handle the case where both `incoming` and `managed` are initialized, but
511-
// with different attributes initialized?
501+
// todo - do we need to specially handle the case where both `incoming` and `managed`
502+
// are initialized, but with different attributes initialized?
512503
// - for now, assume we do not...
513504

514505
// if the managed entity is not a proxy, we can just return it
@@ -533,9 +524,9 @@ private static void markInterceptorDirty(final Object entity, final Object targe
533524
if ( isSelfDirtinessTracker( entity ) && isSelfDirtinessTracker( target ) ) {
534525
// clear, because setting the embedded attributes dirties them
535526
final ManagedEntity managedEntity = asManagedEntity( target );
536-
final boolean useTracker = asManagedEntity( entity ).$$_hibernate_useTracker();
537527
final SelfDirtinessTracker selfDirtinessTrackerTarget = asSelfDirtinessTracker( target );
538-
if ( !selfDirtinessTrackerTarget.$$_hibernate_hasDirtyAttributes() && !useTracker ) {
528+
if ( !selfDirtinessTrackerTarget.$$_hibernate_hasDirtyAttributes()
529+
&& !asManagedEntity( entity ).$$_hibernate_useTracker() ) {
539530
managedEntity.$$_hibernate_setUseTracker( false );
540531
}
541532
else {
@@ -560,10 +551,9 @@ private static boolean isVersionChanged(Object entity, EventSource source, Entit
560551
// an entity to be merged during the same transaction
561552
// (though during a separate operation) in which it was
562553
// originally persisted/saved
563-
boolean changed = !persister.getVersionType().isSame(
564-
persister.getVersion( target ),
565-
persister.getVersion( entity )
566-
);
554+
final boolean changed =
555+
!persister.getVersionType()
556+
.isSame( persister.getVersion( target ), persister.getVersion( entity ) );
567557
// TODO : perhaps we should additionally require that the incoming entity
568558
// version be equivalent to the defined unsaved-value?
569559
return changed && existsInDatabase( target, source, persister );
@@ -577,7 +567,7 @@ private static boolean existsInDatabase(Object entity, EventSource source, Entit
577567
final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
578568
EntityEntry entry = persistenceContext.getEntry( entity );
579569
if ( entry == null ) {
580-
Object id = persister.getIdentifier( entity, source );
570+
final Object id = persister.getIdentifier( entity, source );
581571
if ( id != null ) {
582572
final EntityKey key = source.generateEntityKey( id, persister );
583573
final Object managedEntity = persistenceContext.getEntity( key );
@@ -606,7 +596,6 @@ protected void copyValues(
606596
target,
607597
copyCache
608598
);
609-
610599
persister.setValues( target, copiedValues );
611600
}
612601
}
@@ -618,9 +607,7 @@ protected void copyValues(
618607
final SessionImplementor source,
619608
final MergeContext copyCache,
620609
final ForeignKeyDirection foreignKeyDirection) {
621-
622610
final Object[] copiedValues;
623-
624611
if ( foreignKeyDirection == ForeignKeyDirection.TO_PARENT ) {
625612
// this is the second pass through on a merge op, so here we limit the
626613
// replacement to associations types (value types were already replaced
@@ -646,7 +633,6 @@ protected void copyValues(
646633
foreignKeyDirection
647634
);
648635
}
649-
650636
persister.setValues( target, copiedValues );
651637
}
652638

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.hibernate.cache.spi.access.SoftLock;
1818
import org.hibernate.engine.internal.Cascade;
1919
import org.hibernate.engine.internal.CascadePoint;
20-
import org.hibernate.engine.spi.ActionQueue;
2120
import org.hibernate.engine.spi.CascadingActions;
2221
import org.hibernate.engine.spi.EntityEntry;
2322
import org.hibernate.engine.spi.PersistenceContext;
@@ -32,14 +31,13 @@
3231
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
3332
import org.hibernate.persister.collection.CollectionPersister;
3433
import org.hibernate.persister.entity.EntityPersister;
35-
import org.hibernate.proxy.HibernateProxy;
3634
import org.hibernate.proxy.LazyInitializer;
3735
import org.hibernate.type.CollectionType;
3836
import org.hibernate.type.ComponentType;
39-
import org.hibernate.type.CompositeType;
4037
import org.hibernate.type.Type;
4138

4239
import static org.hibernate.pretty.MessageHelper.infoString;
40+
import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer;
4341

4442
/**
4543
* Defines the default refresh event listener used by hibernate for refreshing entities
@@ -71,14 +69,13 @@ public void onRefresh(RefreshEvent event, RefreshContext refreshedAlready) {
7169
// cascade refresh and the refresh of the parent will take care of initializing the lazy
7270
// entity and setting the correct lock on it, this is needed only when the refresh is called directly on a lazy entity
7371
if ( refreshedAlready.isEmpty() ) {
74-
final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( object );
72+
final LazyInitializer lazyInitializer = extractLazyInitializer( object );
7573
final EntityPersister persister;
7674
if ( lazyInitializer != null ) {
7775
persister = source.getEntityPersister( lazyInitializer.getEntityName(), object );
7876
}
7977
else if ( !isTransient ) {
80-
final EntityEntry entry = persistenceContext.getEntry( object );
81-
persister = entry.getPersister();
78+
persister = persistenceContext.getEntry( object ).getPersister();
8279
}
8380
else {
8481
persister = source.getEntityPersister( source.guessEntityName( object ), object );
@@ -275,7 +272,7 @@ private static Object doRefresh(
275272
if ( result != null ) {
276273
// apply `postRefreshLockMode`, if needed
277274
if ( postRefreshLockMode != null ) {
278-
// if we get here, there was a previous entry, and we need to re-set its lock-mode
275+
// if we get here, there was a previous entry, and we need to reset its lock mode
279276
// - however, the refresh operation actually creates a new entry, so get it
280277
persistenceContext.getEntry( result ).setLockMode( postRefreshLockMode );
281278
}
@@ -312,13 +309,12 @@ private static void evictCachedCollections(EntityPersister persister, Object id,
312309

313310
private static void evictCachedCollections(Type[] types, Object id, EventSource source)
314311
throws HibernateException {
315-
final ActionQueue actionQueue = source.getActionQueue();
316312
final SessionFactoryImplementor factory = source.getFactory();
317-
final MappingMetamodelImplementor metamodel = factory.getRuntimeMetamodels().getMappingMetamodel();
313+
final MappingMetamodelImplementor metamodel = factory.getMappingMetamodel();
318314
for ( Type type : types ) {
319-
if ( type instanceof CollectionType ) {
320-
final String role = ((CollectionType) type).getRole();
321-
final CollectionPersister collectionPersister = metamodel.getCollectionDescriptor( role );
315+
if ( type instanceof CollectionType collectionType ) {
316+
final CollectionPersister collectionPersister =
317+
metamodel.getCollectionDescriptor( collectionType.getRole() );
322318
if ( collectionPersister.hasCache() ) {
323319
final CollectionDataAccess cache = collectionPersister.getCacheAccessStrategy();
324320
final Object ck = cache.generateCacheKey(
@@ -329,12 +325,11 @@ private static void evictCachedCollections(Type[] types, Object id, EventSource
329325
);
330326
final SoftLock lock = cache.lockItem( source, ck, null );
331327
cache.remove( source, ck );
332-
actionQueue.registerProcess( (success, session) -> cache.unlockItem( session, ck, lock ) );
328+
source.getActionQueue().registerProcess( (success, session) -> cache.unlockItem( session, ck, lock ) );
333329
}
334330
}
335-
else if ( type instanceof ComponentType ) {
331+
else if ( type instanceof ComponentType compositeType ) {
336332
// Only components can contain collections
337-
ComponentType compositeType = (ComponentType) type;
338333
evictCachedCollections( compositeType.getSubtypes(), id, source );
339334
}
340335
}

0 commit comments

Comments
 (0)