@@ -247,31 +247,26 @@ private static Object copyCompositeTypeId(
247
247
CompositeType compositeType ,
248
248
EventSource session ,
249
249
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 );
252
252
final Type [] subtypes = compositeType .getSubtypes ();
253
253
final Object [] propertyValues = compositeType .getPropertyValues ( id );
254
254
final Object [] copyValues = compositeType .getPropertyValues ( idCopy );
255
255
for ( int i = 0 ; i < subtypes .length ; i ++ ) {
256
256
final Type subtype = subtypes [i ];
257
257
if ( subtype instanceof EntityType ) {
258
258
// 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 ;
266
261
}
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 );
269
264
}
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 );
272
267
}
273
268
else {
274
- copyValues [i ] = subtype .deepCopy ( propertyValues [i ], sessionFactory );
269
+ copyValues [i ] = subtype .deepCopy ( propertyValues [i ], factory );
275
270
}
276
271
}
277
272
return compositeType .replacePropertyValues ( idCopy , copyValues , session );
@@ -326,14 +321,16 @@ protected void entityIsTransient(MergeEvent event, Object id, MergeContext copyC
326
321
event .setResult ( copy );
327
322
328
323
if ( isPersistentAttributeInterceptable ( copy ) ) {
329
- final PersistentAttributeInterceptor interceptor = asPersistentAttributeInterceptable ( copy ).$$_hibernate_getInterceptor ();
324
+ final PersistentAttributeInterceptor interceptor =
325
+ asPersistentAttributeInterceptable ( copy ).$$_hibernate_getInterceptor ();
330
326
if ( interceptor == null ) {
331
327
persister .getBytecodeEnhancementMetadata ().injectInterceptor ( copy , id , session );
332
328
}
333
329
}
334
330
}
335
331
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 ) {
337
334
final Object existingCopy = copyCache .get ( entity );
338
335
if ( existingCopy != null ) {
339
336
persister .setIdentifier ( existingCopy , id , session );
@@ -352,23 +349,22 @@ private static class CollectionVisitor extends WrapVisitor {
352
349
super ( entity , id , session );
353
350
}
354
351
@ Override
355
- protected Object processCollection (Object collection , CollectionType collectionType ) throws HibernateException {
352
+ protected Object processCollection (Object collection , CollectionType collectionType ) {
356
353
if ( collection instanceof PersistentCollection ) {
357
354
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 );
364
360
if ( !coll .equalsSnapshot ( persister ) ) {
365
361
collectionEntry .resetStoredSnapshot ( coll , coll .getSnapshot ( persister ) );
366
362
}
367
363
}
368
364
return null ;
369
365
}
370
366
@ Override
371
- Object processEntity (Object value , EntityType entityType ) throws HibernateException {
367
+ Object processEntity (Object value , EntityType entityType ) {
372
368
return null ;
373
369
}
374
370
}
@@ -400,13 +396,9 @@ protected void entityIsDetached(MergeEvent event, Object copiedId, Object origin
400
396
if ( originalId == null ) {
401
397
originalId = persister .getIdentifier ( entity , source );
402
398
}
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 ;
410
402
final Object id = getDetachedEntityId ( event , originalId , persister );
411
403
// we must clone embedded composite identifiers, or we will get back the same instance that we pass in
412
404
// 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
482
474
}
483
475
else {
484
476
// 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 () ) ) {
487
478
throw new HibernateException ( "merge requested with id not matching id of passed entity" );
488
479
}
489
480
return id ;
@@ -507,8 +498,8 @@ private static Object unproxyManagedForDetachedMerging(
507
498
final PersistentAttributeInterceptor managedInterceptor =
508
499
asPersistentAttributeInterceptable ( managed ).$$_hibernate_getInterceptor ();
509
500
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?
512
503
// - for now, assume we do not...
513
504
514
505
// 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
533
524
if ( isSelfDirtinessTracker ( entity ) && isSelfDirtinessTracker ( target ) ) {
534
525
// clear, because setting the embedded attributes dirties them
535
526
final ManagedEntity managedEntity = asManagedEntity ( target );
536
- final boolean useTracker = asManagedEntity ( entity ).$$_hibernate_useTracker ();
537
527
final SelfDirtinessTracker selfDirtinessTrackerTarget = asSelfDirtinessTracker ( target );
538
- if ( !selfDirtinessTrackerTarget .$$_hibernate_hasDirtyAttributes () && !useTracker ) {
528
+ if ( !selfDirtinessTrackerTarget .$$_hibernate_hasDirtyAttributes ()
529
+ && !asManagedEntity ( entity ).$$_hibernate_useTracker () ) {
539
530
managedEntity .$$_hibernate_setUseTracker ( false );
540
531
}
541
532
else {
@@ -560,10 +551,9 @@ private static boolean isVersionChanged(Object entity, EventSource source, Entit
560
551
// an entity to be merged during the same transaction
561
552
// (though during a separate operation) in which it was
562
553
// 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 ) );
567
557
// TODO : perhaps we should additionally require that the incoming entity
568
558
// version be equivalent to the defined unsaved-value?
569
559
return changed && existsInDatabase ( target , source , persister );
@@ -577,7 +567,7 @@ private static boolean existsInDatabase(Object entity, EventSource source, Entit
577
567
final PersistenceContext persistenceContext = source .getPersistenceContextInternal ();
578
568
EntityEntry entry = persistenceContext .getEntry ( entity );
579
569
if ( entry == null ) {
580
- Object id = persister .getIdentifier ( entity , source );
570
+ final Object id = persister .getIdentifier ( entity , source );
581
571
if ( id != null ) {
582
572
final EntityKey key = source .generateEntityKey ( id , persister );
583
573
final Object managedEntity = persistenceContext .getEntity ( key );
@@ -606,7 +596,6 @@ protected void copyValues(
606
596
target ,
607
597
copyCache
608
598
);
609
-
610
599
persister .setValues ( target , copiedValues );
611
600
}
612
601
}
@@ -618,9 +607,7 @@ protected void copyValues(
618
607
final SessionImplementor source ,
619
608
final MergeContext copyCache ,
620
609
final ForeignKeyDirection foreignKeyDirection ) {
621
-
622
610
final Object [] copiedValues ;
623
-
624
611
if ( foreignKeyDirection == ForeignKeyDirection .TO_PARENT ) {
625
612
// this is the second pass through on a merge op, so here we limit the
626
613
// replacement to associations types (value types were already replaced
@@ -646,7 +633,6 @@ protected void copyValues(
646
633
foreignKeyDirection
647
634
);
648
635
}
649
-
650
636
persister .setValues ( target , copiedValues );
651
637
}
652
638
0 commit comments