Search in sources :

Example 6 with PersistentCollectionChangeData

use of org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData in project hibernate-orm by hibernate.

the class AbstractCollectionMapper method mapCollectionChanges.

/**
 * Map collection changes using the collection element type equality functionality.
 *
 * @param session The session.
 * @param newColl The new persistent collection.
 * @param oldColl The old collection.
 * @param id The owning entity identifier.
 * @param collectionPersister The collection persister.
 * @return the persistent collection changes.
 */
private List<PersistentCollectionChangeData> mapCollectionChanges(SessionImplementor session, PersistentCollection newColl, Serializable oldColl, Serializable id, CollectionPersister collectionPersister) {
    final List<PersistentCollectionChangeData> collectionChanges = new ArrayList<PersistentCollectionChangeData>();
    // Comparing new and old collection content.
    final Collection newCollection = getNewCollectionContent(newColl);
    final Collection oldCollection = getOldCollectionContent(oldColl);
    // take the new collection and remove any that exist in the old collection.
    // take the resulting Set<> and generate ADD changes.
    final Set<Object> added = buildCollectionChangeSet(newColl, newCollection);
    if (oldColl != null && collectionPersister != null) {
        for (Object object : oldCollection) {
            for (Iterator addedIt = added.iterator(); addedIt.hasNext(); ) {
                Object object2 = addedIt.next();
                if (collectionPersister.getElementType().isSame(object, object2)) {
                    addedIt.remove();
                    break;
                }
            }
        }
    }
    addCollectionChanges(session, collectionChanges, added, RevisionType.ADD, id);
    // take the old collection and remove any that exist in the new collection.
    // take the resulting Set<> and generate DEL changes.
    final Set<Object> deleted = buildCollectionChangeSet(oldColl, oldCollection);
    if (newColl != null && collectionPersister != null) {
        for (Object object : newCollection) {
            for (Iterator deletedIt = deleted.iterator(); deletedIt.hasNext(); ) {
                Object object2 = deletedIt.next();
                if (collectionPersister.getElementType().isSame(object, object2)) {
                    deletedIt.remove();
                    break;
                }
            }
        }
    }
    addCollectionChanges(session, collectionChanges, deleted, RevisionType.DEL, id);
    return collectionChanges;
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Collection(java.util.Collection) PersistentCollectionChangeData(org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData)

Example 7 with PersistentCollectionChangeData

use of org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData in project hibernate-orm by hibernate.

the class AbstractCollectionMapper method mapCollectionChanges.

/**
 * Map collection changes using hash identity.
 *
 * @param session The session.
 * @param newColl The new persistent collection.
 * @param oldColl The old collection.
 * @param id The owning entity identifier.
 * @return the persistent collection changes.
 */
@SuppressWarnings("unchecked")
private List<PersistentCollectionChangeData> mapCollectionChanges(SessionImplementor session, PersistentCollection newColl, Serializable oldColl, Serializable id) {
    final List<PersistentCollectionChangeData> collectionChanges = new ArrayList<PersistentCollectionChangeData>();
    // Comparing new and old collection content.
    final Collection newCollection = getNewCollectionContent(newColl);
    final Collection oldCollection = getOldCollectionContent(oldColl);
    final Set<Object> added = buildCollectionChangeSet(newColl, newCollection);
    // removeAll in AbstractSet has an implementation that is hashcode-change sensitive (as opposed to addAll).
    if (oldColl != null) {
        added.removeAll(new HashSet(oldCollection));
    }
    addCollectionChanges(session, collectionChanges, added, RevisionType.ADD, id);
    final Set<Object> deleted = buildCollectionChangeSet(oldColl, oldCollection);
    // The same as above - re-hashing new collection.
    if (newColl != null) {
        deleted.removeAll(new HashSet(newCollection));
    }
    addCollectionChanges(session, collectionChanges, deleted, RevisionType.DEL, id);
    return collectionChanges;
}
Also used : ArrayList(java.util.ArrayList) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Collection(java.util.Collection) PersistentCollectionChangeData(org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData) HashSet(java.util.HashSet)

Aggregations

PersistentCollectionChangeData (org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData)7 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Serializable (java.io.Serializable)2 Collection (java.util.Collection)2 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)2 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)2 CollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUnit)2 PersistentCollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit)2 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 RevisionType (org.hibernate.envers.RevisionType)1 AuditEntitiesConfiguration (org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration)1 AuditWorkUnit (org.hibernate.envers.internal.synchronization.work.AuditWorkUnit)1 FakeBidirectionalRelationWorkUnit (org.hibernate.envers.internal.synchronization.work.FakeBidirectionalRelationWorkUnit)1