Retrieving old values ​​in a collection on the onPostUpdateCollection event in sleep mode

I use hibernate 4.2 for persistent storage. I am implementing event listeners in sleep mode to receive notification when a specific object changes. Implemented An event PostUpdateEventListenerinjection in hibernate, but it does not call the method while updating the values ​​of the collection. It is currently implemented PostCollectionUpdateEventListener, which runs the method when updating the collection.

The class is as follows

public class Employee {
  private int id;
  private String name;
  private Set<Address> addresses;

  //all getters and setters are implemented.
}

public class Address {
  private int id;
  private String street;
  private String city;

  //all getters and setters are implemented.
}

I implemented a display as xml files with all the display and the subsequent display set

In Employee.hbm.xml

<hibernate-mapping>
  <class name="Employee">
  ... all mappings
  <set name="addresses" inverse="true" cascade="all-delete-orphan">
    <key column="Emp_id"/>
    <one-to-many class="Address"/>
  </set>
</hibernate-mapping>

The .hbm.xml Address file is correctly implemented.

Standby Sleep

public void onPostUpdateCollection(PostCollectionUpdateEvent event) {
  Object obj = event.getAffectedOwnerOrNull();
  //this gives me updated values.

  I want now code to get old collection values which going to be deleted.
}

I tried the following line

PersistentCollection collection = event.getCollection();
// this gives new update collection values of addresses

I saw the method in PersistentCollection Serializable getStoredSnapshot(), but it gives null values.

, , , . , onPostUpdateCollection() Employee.

: ? , . .

+1
2

PostCollectionUpdateEventListener . PreCollectionUpdateEventListener

public void onPreUpdateCollection(PreCollectionUpdateEvent event) {
  PersistentCollection collection = event.getCollection();
  HashMap snapshot = (HashMap) collection.getStoredSnapshot();
  //set values are also stored as map values with same key and value as set value
  Set<Map.Entry> set = snapshot.entrySet();
  //Now this set contains key value of old collection values before update
}
+3

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;

    /**
     * Constructs a CollectionUpdateAction
     *
     * @param collection The collection to update
     * @param persister The collection persister
     * @param id The collection key
     * @param emptySnapshot Indicates if the snapshot is empty
     * @param session The session
     */
    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");
            }
        }
    }
}
+1

Source: https://habr.com/ru/post/1707488/


All Articles