Search in sources :

Example 26 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testUpdateParentOneChildDiffCollectionSameChild.

@Test
public void testUpdateParentOneChildDiffCollectionSameChild() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    Child child = (Child) parent.getChildren().iterator().next();
    listeners.clear();
    assertEquals(1, parent.getChildren().size());
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    if (child instanceof Entity) {
        child = (Child) s.get(child.getClass(), ((Entity) child).getId());
    }
    Collection oldCollection = parent.getChildren();
    parent.newChildren(createCollection());
    parent.addChild(child);
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) oldCollection).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, oldCollection, index++);
    }
    if (child instanceof ChildWithBidirectionalManyToMany) {
        ChildWithBidirectionalManyToMany childWithManyToMany = (ChildWithBidirectionalManyToMany) child;
        if (((PersistentCollection) childWithManyToMany.getParents()).wasInitialized()) {
            checkResult(listeners, listeners.getInitializeCollectionListener(), childWithManyToMany, index++);
        }
    }
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), parent, oldCollection, index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), parent, oldCollection, index++);
    if (child instanceof ChildWithBidirectionalManyToMany) {
        // hmmm, the same parent was removed and re-added to the child's collection;
        // should this be considered an update?
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) child, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) child, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRecreateListener(), parent, index++);
    checkResult(listeners, listeners.getPostCollectionRecreateListener(), parent, index++);
    checkNumberOfResults(listeners, index);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Transaction(org.hibernate.Transaction) Collection(java.util.Collection) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) Session(org.hibernate.Session) Test(org.junit.Test)

Example 27 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project dhis2-core by dhis2.

the class DefaultLinkService method generateLink.

private <T> void generateLink(T object, String hrefBase, boolean deepScan) {
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    if (schema == null) {
        log.warn("Could not find schema for object of type " + object.getClass().getName() + ".");
        return;
    }
    generateHref(object, hrefBase);
    if (!deepScan) {
        return;
    }
    for (Property property : schema.getProperties()) {
        try {
            // TODO should we support non-idObjects?
            if (property.isIdentifiableObject()) {
                Object propertyObject = property.getGetterMethod().invoke(object);
                if (propertyObject == null) {
                    continue;
                }
                // unwrap hibernate PersistentCollection
                if (PersistentCollection.class.isAssignableFrom(propertyObject.getClass())) {
                    PersistentCollection collection = (PersistentCollection) propertyObject;
                    propertyObject = collection.getValue();
                }
                if (!property.isCollection()) {
                    generateHref(propertyObject, hrefBase);
                } else {
                    Collection<?> collection = (Collection<?>) propertyObject;
                    for (Object collectionObject : collection) {
                        generateHref(collectionObject, hrefBase);
                    }
                }
            }
        } catch (InvocationTargetException | IllegalAccessException ignored) {
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Schema(org.hisp.dhis.schema.Schema) Collection(java.util.Collection) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Property(org.hisp.dhis.schema.Property) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 28 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection 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 = new HashSet<>();
    if (newColl != null) {
        added.addAll(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 = new HashSet<>();
    if (oldColl != null) {
        deleted.addAll(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)

Example 29 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class AbstractCollectionMapper method isFromNullToEmptyOrFromEmptyToNull.

private boolean isFromNullToEmptyOrFromEmptyToNull(PersistentCollection newColl, Serializable oldColl) {
    // Comparing new and old collection content.
    final Collection newCollection = getNewCollectionContent(newColl);
    final Collection oldCollection = getOldCollectionContent(oldColl);
    return oldCollection == null && newCollection != null && newCollection.isEmpty() || newCollection == null && oldCollection != null && oldCollection.isEmpty();
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Collection(java.util.Collection)

Example 30 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection 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 = new HashSet<>();
    if (newColl != null) {
        added.addAll(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 = new HashSet<>();
    if (oldColl != null) {
        deleted.addAll(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) HashSet(java.util.HashSet)

Aggregations

PersistentCollection (org.hibernate.collection.spi.PersistentCollection)53 Session (org.hibernate.Session)20 Test (org.junit.Test)20 Transaction (org.hibernate.Transaction)16 ChildWithBidirectionalManyToMany (org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany)15 Collection (java.util.Collection)13 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)11 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)8 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)7 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 Serializable (java.io.Serializable)6 CollectionKey (org.hibernate.engine.spi.CollectionKey)6 HibernateException (org.hibernate.HibernateException)5 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)5 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)5 Map (java.util.Map)4 IdentityMap (org.hibernate.internal.util.collections.IdentityMap)4 TestForIssue (org.hibernate.testing.TestForIssue)4 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3