Skip to content

Commit 96f1734

Browse files
committed
allow an EntityCopyObserverFactory in MERGE_ENTITY_COPY_OBSERVER setting
1 parent 516deb4 commit 96f1734

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ public void entityCopyDetected(
4545
}
4646

4747
private String getManagedOrDetachedEntityString(Object managedEntity, Object entity ) {
48-
if ( entity == managedEntity) {
49-
return "Managed: [" + entity + "]";
50-
}
51-
else {
52-
return "Detached: [" + entity + "]";
53-
}
48+
return entity == managedEntity
49+
? "Managed: [" + entity + "]"
50+
: "Detached: [" + entity + "]";
5451
}
5552

5653
public void clear() {

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
/**
2020
* Looks for the configuration property {@value AvailableSettings#MERGE_ENTITY_COPY_OBSERVER} and registers
21-
* the matching {@link EntityCopyObserverFactory} based on the configuration value.
21+
* the matching {@link EntityCopyObserverFactory} based on the configuration observerClass.
2222
* <p>
2323
* For known implementations some optimisations are possible, such as reusing a singleton for the stateless
2424
* implementations. When a user plugs in a custom {@link EntityCopyObserver} we take a defensive approach.
@@ -32,7 +32,10 @@ public class EntityCopyObserverFactoryInitiator implements StandardServiceInitia
3232
@Override
3333
public EntityCopyObserverFactory initiateService(final Map<String, Object> configurationValues, final ServiceRegistryImplementor registry) {
3434
final Object value = getConfigurationValue( configurationValues );
35-
if ( value.equals( EntityCopyNotAllowedObserver.SHORT_NAME )
35+
if ( value instanceof EntityCopyObserverFactory factory ) {
36+
return factory;
37+
}
38+
else if ( value.equals( EntityCopyNotAllowedObserver.SHORT_NAME )
3639
|| value.equals( EntityCopyNotAllowedObserver.class.getName() ) ) {
3740
LOG.debugf( "Configured EntityCopyObserver strategy: %s", EntityCopyNotAllowedObserver.SHORT_NAME );
3841
return EntityCopyNotAllowedObserver.FACTORY_OF_SELF;
@@ -48,15 +51,16 @@ else if ( value.equals( EntityCopyAllowedLoggedObserver.SHORT_NAME )
4851
return EntityCopyAllowedLoggedObserver.FACTORY_OF_SELF;
4952
}
5053
else {
51-
//We load an "example instance" just to get its Class;
52-
//this might look excessive, but it also happens to test eagerly (at boot) that we can actually construct these
53-
//and that they are indeed of the right type.
54+
// We load an "example instance" just to get its Class;
55+
// this might look excessive, but it also happens to test eagerly
56+
// (at boot) that we can actually construct these and that they
57+
// are indeed of the right type.
5458
final EntityCopyObserver exampleInstance =
5559
registry.requireService( StrategySelector.class )
5660
.resolveStrategy( EntityCopyObserver.class, value );
57-
final Class<?> observerType = exampleInstance.getClass();
58-
LOG.debugf( "Configured EntityCopyObserver is a custom implementation of type %s", observerType.getName() );
59-
return new EntityObserversFactoryFromClass( observerType );
61+
final Class<? extends EntityCopyObserver> observerType = exampleInstance.getClass();
62+
LOG.debugf( "Configured EntityCopyObserver is a custom implementation of type '%s'", observerType.getName() );
63+
return new EntityCopyObserverFactoryFromClass( observerType );
6064
}
6165
}
6266

@@ -78,23 +82,17 @@ public Class<EntityCopyObserverFactory> getServiceInitiated() {
7882
return EntityCopyObserverFactory.class;
7983
}
8084

81-
private static class EntityObserversFactoryFromClass implements EntityCopyObserverFactory {
82-
83-
private final Class<?> value;
84-
85-
public EntityObserversFactoryFromClass(Class<?> value) {
86-
this.value = value;
87-
}
85+
private record EntityCopyObserverFactoryFromClass(Class<? extends EntityCopyObserver> observerClass)
86+
implements EntityCopyObserverFactory {
8887

8988
@Override
90-
public EntityCopyObserver createEntityCopyObserver() {
91-
try {
92-
return (EntityCopyObserver) value.newInstance();
93-
}
94-
catch (Exception e) {
95-
throw new HibernateException( "Could not instantiate class of type " + value.getName() );
89+
public EntityCopyObserver createEntityCopyObserver() {
90+
try {
91+
return observerClass.newInstance();
92+
}
93+
catch (Exception e) {
94+
throw new HibernateException( "Could not instantiate class of type " + observerClass.getName() );
95+
}
9696
}
9797
}
98-
}
99-
10098
}

hibernate-core/src/main/java/org/hibernate/event/spi/EntityCopyObserver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public interface EntityCopyObserver {
2424
void entityCopyDetected(Object managedEntity, Object mergeEntity1, Object mergeEntity2, EventSource session);
2525

2626
/**
27-
* Called when the top-level merge operation is complete.
27+
* Called when the toplevel merge operation is complete.
2828
*
2929
* @param session The session
3030
*/
3131
void topLevelMergeComplete(EventSource session);
3232

3333
/**
34-
* Called to clear any data stored in this EntityCopyObserver.
34+
* Called to clear any data stored in this {@code EntityCopyObserver}.
3535
*/
3636
void clear();
3737
}

hibernate-core/src/main/java/org/hibernate/event/spi/EntityCopyObserverFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import org.hibernate.service.Service;
88

9+
/**
10+
* A {@linkplain Service service} which creates new instances of {@link EntityCopyObserver}.
11+
*/
912
@FunctionalInterface
1013
public interface EntityCopyObserverFactory extends Service {
1114
EntityCopyObserver createEntityCopyObserver();

0 commit comments

Comments
 (0)