Skip to content

Commit 05a7d1e

Browse files
committed
HHH-6882 - Expose CollectionPersister from AbstractCollectionEvent
1 parent 82b1bc8 commit 05a7d1e

9 files changed

+169
-92
lines changed

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

Lines changed: 88 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,124 @@
44
*/
55
package org.hibernate.event.spi;
66

7-
import org.hibernate.collection.spi.PersistentCollection;
7+
import org.hibernate.Internal;import org.hibernate.collection.spi.PersistentCollection;
88
import org.hibernate.persister.collection.CollectionPersister;
99

1010
/**
1111
* Defines a base class for events involving collections.
1212
*
1313
* @author Gail Badner
14+
* @author Steve Ebersole
15+
*
16+
* @apiNote AbstractCollectionEvent and its implementations are defined
17+
* as SPI for consumption; their constructors are defined as
18+
* {@linkplain Internal internal}
1419
*/
1520
public abstract class AbstractCollectionEvent extends AbstractSessionEvent {
16-
1721
private final PersistentCollection<?> collection;
18-
private final Object affectedOwner;
19-
private final Object affectedOwnerId;
20-
private final String affectedOwnerEntityName;
22+
private final CollectionPersister collectionPersister;
23+
private final Object owner;
24+
private final Object ownerId;
25+
private final String ownerEntityName;
2126

2227
/**
2328
* Constructs an instance for a stateful session.
24-
* @param collection - the collection
25-
* @param source - the Session source
26-
* @param affectedOwner - the owner that is affected by this event;
27-
* can be null if unavailable
28-
* @param affectedOwnerId - the ID for the owner that is affected
29-
* by this event; can be null if unavailable
29+
*
30+
* @param collectionPersister The descriptor for the collection mapping.
31+
* @param collection - The (wrapped) collection instance.
32+
* @param source - The {@linkplain org.hibernate.Session session}
33+
* @param owner - The entity instance that "owns" the {@code collection}
34+
* affected by this event; can be {@code null} if unavailable.
35+
* @param ownerId - The identifier value for the {@code owner}; can be
36+
* {@code null} if unavailable.
3037
*/
38+
@Internal
3139
public AbstractCollectionEvent(
3240
CollectionPersister collectionPersister,
3341
PersistentCollection<?> collection,
3442
EventSource source,
35-
Object affectedOwner,
36-
Object affectedOwnerId) {
43+
Object owner,
44+
Object ownerId) {
3745
super( source );
3846
this.collection = collection;
39-
this.affectedOwner = affectedOwner;
40-
this.affectedOwnerId = affectedOwnerId;
41-
this.affectedOwnerEntityName =
42-
getAffectedOwnerEntityName( collectionPersister, affectedOwner, source );
47+
this.collectionPersister = collectionPersister;
48+
this.owner = owner;
49+
this.ownerId = ownerId;
50+
this.ownerEntityName = getAffectedOwnerEntityName( collectionPersister, owner, source );
4351
}
4452

4553
/**
4654
* Constructs an instance for a stateless session.
47-
* @param collection - the collection
48-
* @param entityName - the name of the owning entity
49-
* @param affectedOwner - the owner that is affected by this event;
50-
* can be null if unavailable
51-
* @param affectedOwnerId - the ID for the owner that is affected
52-
* by this event; can be null if unavailable
55+
*
56+
* @param collectionPersister The descriptor for the collection mapping.
57+
* @param collection - The (wrapped) collection instance.
58+
* @param ownerEntityName - The entity-name of the {@code owner}.
59+
* @param owner - The entity instance that "owns" the {@code collection}
60+
* affected by this event; can be {@code null} if unavailable.
61+
* @param ownerId - The identifier value for the {@code owner}; can be
62+
* {@code null} if unavailable.
5363
*/
64+
@Internal
5465
public AbstractCollectionEvent(
66+
CollectionPersister collectionPersister,
5567
PersistentCollection<?> collection,
56-
String entityName,
57-
Object affectedOwner,
58-
Object affectedOwnerId) {
68+
String ownerEntityName,
69+
Object owner,
70+
Object ownerId) {
5971
super( null );
6072
this.collection = collection;
61-
this.affectedOwner = affectedOwner;
62-
this.affectedOwnerId = affectedOwnerId;
63-
this.affectedOwnerEntityName = entityName;
73+
this.owner = owner;
74+
this.ownerId = ownerId;
75+
this.ownerEntityName = ownerEntityName;
76+
this.collectionPersister = collectionPersister;
77+
}
78+
79+
/**
80+
* The descriptor for the {@linkplain #getCollection() collection} mapping.
81+
*/
82+
public CollectionPersister getCollectionPersister() {
83+
return collectionPersister;
84+
}
85+
86+
/**
87+
* The (wrapped) collection instance affected by this event.
88+
*/
89+
public PersistentCollection<?> getCollection() {
90+
return collection;
6491
}
6592

66-
protected static CollectionPersister getLoadedCollectionPersister( PersistentCollection<?> collection, EventSource source ) {
93+
/**
94+
* Get the collection owner entity that is affected by this event.
95+
*
96+
* @return the affected owner; returns null if the entity is not in the persistence context
97+
* (e.g., because the collection from a detached entity was moved to a new owner)
98+
*/
99+
public Object getAffectedOwnerOrNull() {
100+
return owner;
101+
}
102+
103+
/**
104+
* Get the ID for the collection owner entity that is affected by this event.
105+
*
106+
* @return the affected owner ID; returns null if the ID cannot be obtained
107+
* from the collection's loaded key (e.g., a property-ref is used for the
108+
* collection and does not include the entity's ID)
109+
*/
110+
public Object getAffectedOwnerIdOrNull() {
111+
return ownerId;
112+
}
113+
114+
/**
115+
* Get the entity name for the collection owner entity that is affected by this event.
116+
*
117+
* @return the entity name; if the owner is not in the PersistenceContext, the
118+
* returned value may be a superclass name, instead of the actual class name
119+
*/
120+
public String getAffectedOwnerEntityName() {
121+
return ownerEntityName;
122+
}
123+
124+
protected static CollectionPersister getLoadedCollectionPersister(PersistentCollection<?> collection, EventSource source) {
67125
final var entry = source.getPersistenceContextInternal().getCollectionEntry( collection );
68126
return entry == null ? null : entry.getLoadedPersister();
69127
}
@@ -102,39 +160,4 @@ protected static String getAffectedOwnerEntityName(
102160
: null;
103161
}
104162

105-
public PersistentCollection<?> getCollection() {
106-
return collection;
107-
}
108-
109-
/**
110-
* Get the collection owner entity that is affected by this event.
111-
*
112-
* @return the affected owner; returns null if the entity is not in the persistence context
113-
* (e.g., because the collection from a detached entity was moved to a new owner)
114-
*/
115-
public Object getAffectedOwnerOrNull() {
116-
return affectedOwner;
117-
}
118-
119-
/**
120-
* Get the ID for the collection owner entity that is affected by this event.
121-
*
122-
* @return the affected owner ID; returns null if the ID cannot be obtained
123-
* from the collection's loaded key (e.g., a property-ref is used for the
124-
* collection and does not include the entity's ID)
125-
*/
126-
public Object getAffectedOwnerIdOrNull() {
127-
return affectedOwnerId;
128-
}
129-
130-
/**
131-
* Get the entity name for the collection owner entity that is affected by this event.
132-
*
133-
* @return the entity name; if the owner is not in the PersistenceContext, the
134-
* returned value may be a superclass name, instead of the actual class name
135-
*/
136-
public String getAffectedOwnerEntityName() {
137-
return affectedOwnerEntityName;
138-
}
139-
140163
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.event.spi;
66

7+
import org.hibernate.Internal;
78
import org.hibernate.collection.spi.PersistentCollection;
89

910
/**
@@ -14,6 +15,7 @@
1415
*/
1516
public class InitializeCollectionEvent extends AbstractCollectionEvent {
1617

18+
@Internal
1719
public InitializeCollectionEvent(PersistentCollection<?> collection, EventSource source ) {
1820
super(
1921
getLoadedCollectionPersister( collection, source ),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.event.spi;
66

7+
import org.hibernate.Internal;
78
import org.hibernate.collection.spi.PersistentCollection;
89
import org.hibernate.persister.collection.CollectionPersister;
910

@@ -14,6 +15,7 @@
1415
*/
1516
public class PostCollectionRecreateEvent extends AbstractCollectionEvent {
1617

18+
@Internal
1719
public PostCollectionRecreateEvent(
1820
CollectionPersister collectionPersister,
1921
PersistentCollection<?> collection,
@@ -27,11 +29,13 @@ public PostCollectionRecreateEvent(
2729
);
2830
}
2931

32+
@Internal
3033
public PostCollectionRecreateEvent(
34+
CollectionPersister collectionPersister,
3135
PersistentCollection<?> collection,
3236
Object id,
3337
String entityName,
3438
Object loadedOwner) {
35-
super( collection, entityName, loadedOwner, id );
39+
super( collectionPersister, collection, entityName, loadedOwner, id );
3640
}
3741
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.event.spi;
66

7+
import org.hibernate.Internal;
78
import org.hibernate.collection.spi.PersistentCollection;
89
import org.hibernate.persister.collection.CollectionPersister;
910

@@ -13,6 +14,7 @@
1314
* @author Gail Badner
1415
*/
1516
public class PostCollectionRemoveEvent extends AbstractCollectionEvent {
17+
@Internal
1618
public PostCollectionRemoveEvent(
1719
CollectionPersister collectionPersister,
1820
PersistentCollection<?> collection,
@@ -27,11 +29,13 @@ public PostCollectionRemoveEvent(
2729
);
2830
}
2931

32+
@Internal
3033
public PostCollectionRemoveEvent(
34+
CollectionPersister collectionPersister,
3135
PersistentCollection<?> collection,
3236
Object id,
3337
String entityName,
3438
Object loadedOwner) {
35-
super( collection, entityName, loadedOwner, id );
39+
super( collectionPersister, collection, entityName, loadedOwner, id );
3640
}
3741
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.event.spi;
66

7+
import org.hibernate.Internal;
78
import org.hibernate.collection.spi.PersistentCollection;
89
import org.hibernate.persister.collection.CollectionPersister;
910

@@ -13,6 +14,8 @@
1314
* @author Gail Badner
1415
*/
1516
public class PostCollectionUpdateEvent extends AbstractCollectionEvent {
17+
18+
@Internal
1619
public PostCollectionUpdateEvent(
1720
CollectionPersister collectionPersister,
1821
PersistentCollection<?> collection,
@@ -26,12 +29,13 @@ public PostCollectionUpdateEvent(
2629
);
2730
}
2831

29-
32+
@Internal
3033
public PostCollectionUpdateEvent(
34+
CollectionPersister collectionPersister,
3135
PersistentCollection<?> collection,
3236
Object id,
3337
String entityName,
3438
Object loadedOwner) {
35-
super( collection, entityName, loadedOwner, id );
39+
super( collectionPersister, collection, entityName, loadedOwner, id );
3640
}
3741
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.event.spi;
66

7+
import org.hibernate.Internal;
78
import org.hibernate.collection.spi.PersistentCollection;
89
import org.hibernate.persister.collection.CollectionPersister;
910

@@ -14,6 +15,7 @@
1415
*/
1516
public class PreCollectionRecreateEvent extends AbstractCollectionEvent {
1617

18+
@Internal
1719
public PreCollectionRecreateEvent(
1820
CollectionPersister collectionPersister,
1921
PersistentCollection<?> collection,
@@ -27,12 +29,13 @@ public PreCollectionRecreateEvent(
2729
);
2830
}
2931

30-
32+
@Internal
3133
public PreCollectionRecreateEvent(
34+
CollectionPersister collectionPersister,
3235
PersistentCollection<?> collection,
3336
Object id,
3437
String entityName,
3538
Object loadedOwner) {
36-
super( collection, entityName, loadedOwner, id );
39+
super( collectionPersister, collection, entityName, loadedOwner, id );
3740
}
3841
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.event.spi;
66

7+
import org.hibernate.Internal;
78
import org.hibernate.collection.spi.PersistentCollection;
89
import org.hibernate.persister.collection.CollectionPersister;
910

@@ -14,6 +15,7 @@
1415
*/
1516
public class PreCollectionRemoveEvent extends AbstractCollectionEvent {
1617

18+
@Internal
1719
public PreCollectionRemoveEvent(
1820
CollectionPersister collectionPersister,
1921
PersistentCollection<?> collection,
@@ -28,11 +30,13 @@ public PreCollectionRemoveEvent(
2830
);
2931
}
3032

33+
@Internal
3134
public PreCollectionRemoveEvent(
35+
CollectionPersister collectionPersister,
3236
PersistentCollection<?> collection,
3337
Object id,
3438
String entityName,
3539
Object loadedOwner) {
36-
super( collection, entityName, loadedOwner, id );
40+
super( collectionPersister, collection, entityName, loadedOwner, id );
3741
}
3842
}

0 commit comments

Comments
 (0)