PostCollectionUpdateEvent, CollectionUpdateAction.
5.1.3.Final. , oldObj, PostCollectionUpdateEvent.
package org.hibernate.event.spi;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.persister.collection.CollectionPersister;
public class MyPostCollectionUpdateEvent extends PostCollectionUpdateEvent {
private static final long serialVersionUID = 1L;
private Object oldObj;
public MyPostCollectionUpdateEvent(Object oldObj, CollectionPersister collectionPersister, PersistentCollection collection, EventSource source) {
super(collectionPersister, collection, source);
this.oldObj = oldObj;
}
public Object getOldObj() {
return oldObj;
}
public void setOldObj(Object oldObj) {
this.oldObj = oldObj;
}
}
CollectionUpdateAction - , . , org.hibernate.action.internal CollectionUpdateAction hibernate-orm , , .
public class CollectionUpdateAction extends CollectionAction {
private final boolean emptySnapshot;
private Object oldObject;
public CollectionUpdateAction(
final PersistentCollection collection,
final CollectionPersister persister,
final Serializable id,
final boolean emptySnapshot,
final SessionImplementor session) {
super( persister, collection, id, session );
this.emptySnapshot = emptySnapshot;
}
@Override
public void execute() throws HibernateException {
final Serializable id = getKey();
final SessionImplementor session = getSession();
final CollectionPersister persister = getPersister();
final PersistentCollection collection = getCollection();
final boolean affectedByFilters = persister.isAffectedByEnabledFilters( session );
preUpdate();
this.oldObject = session.getPersistenceContext().getSnapshot(collection);
...
...
...
}
private void postUpdate() {
final EventListenerGroup<PostCollectionUpdateEventListener> listenerGroup = listenerGroup( EventType.POST_COLLECTION_UPDATE );
if ( listenerGroup.isEmpty() ) {
return;
}
final MyPostCollectionUpdateEvent event = new MyPostCollectionUpdateEvent(
oldObject,
getPersister(),
getCollection(),
eventSource()
);
for ( PostCollectionUpdateEventListener listener : listenerGroup.listeners() ) {
listener.onPostUpdateCollection( event );
}
}
}
, , hibernate jar . , ,
public class DummyCollectionEventListener implements PostCollectionUpdateEventListener {
@Override
public void onPostUpdateCollection(PostCollectionUpdateEvent event) {
if (event instanceof MyPostCollectionUpdateEvent) {
Object oldObjects = ((MyPostCollectionUpdateEvent) event).getOldObj();
PersistentCollection persistentCollection = event.getCollection();
String role = persistentCollection.getRole();
String propertyName = role.substring(role.lastIndexOf(".") + 1, role.length());
System.out.println("property name : " + propertyName);
if (persistentCollection instanceof PersistentList && oldObjects instanceof List) {
System.out.println("Old Object List : " + ((List<?>) oldObjects));
} else if (oldObjects instanceof Map<?, ?>) {
Map<?, ?> props = (Map<?, ?>) oldObjects;
if (persistentCollection instanceof PersistentMap)
System.out.println("Old Object Map : " + props);
else if (persistentCollection instanceof PersistentSet)
System.out.println("Old Object Set : " + props.keySet());
else
System.out.println("Unknown Class type : " + persistentCollection.getClass());
} else {
System.out.println("Unknown event");
}
}
}
}