Search in sources :

Example 6 with IdentitySet

use of org.hibernate.internal.util.collections.IdentitySet in project hibernate-orm by hibernate.

the class AbstractPersistentCollection method getOrphans.

/**
	 * Given a collection of entity instances that used to
	 * belong to the collection, and a collection of instances
	 * that currently belong, return a collection of orphans
	 */
@SuppressWarnings({ "JavaDoc", "unchecked" })
protected static Collection getOrphans(Collection oldElements, Collection currentElements, String entityName, SharedSessionContractImplementor session) throws HibernateException {
    // short-circuit(s)
    if (currentElements.size() == 0) {
        // no new elements, the old list contains only Orphans
        return oldElements;
    }
    if (oldElements.size() == 0) {
        // no old elements, so no Orphans neither
        return oldElements;
    }
    final EntityPersister entityPersister = session.getFactory().getEntityPersister(entityName);
    final Type idType = entityPersister.getIdentifierType();
    final boolean useIdDirect = mayUseIdDirect(idType);
    // create the collection holding the Orphans
    final Collection res = new ArrayList();
    // collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access
    final java.util.Set currentIds = new HashSet();
    final java.util.Set currentSaving = new IdentitySet();
    for (Object current : currentElements) {
        if (current != null && ForeignKeys.isNotTransient(entityName, current, null, session)) {
            final EntityEntry ee = session.getPersistenceContext().getEntry(current);
            if (ee != null && ee.getStatus() == Status.SAVING) {
                currentSaving.add(current);
            } else {
                final Serializable currentId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, current, session);
                currentIds.add(useIdDirect ? currentId : new TypedValue(idType, currentId));
            }
        }
    }
    // iterate over the *old* list
    for (Object old : oldElements) {
        if (!currentSaving.contains(old)) {
            final Serializable oldId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, old, session);
            if (!currentIds.contains(useIdDirect ? oldId : new TypedValue(idType, oldId))) {
                res.add(old);
            }
        }
    }
    return res;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) IdentitySet(org.hibernate.internal.util.collections.IdentitySet) ArrayList(java.util.ArrayList) PostgresUUIDType(org.hibernate.type.PostgresUUIDType) UUIDBinaryType(org.hibernate.type.UUIDBinaryType) CompositeType(org.hibernate.type.CompositeType) IntegerType(org.hibernate.type.IntegerType) StringType(org.hibernate.type.StringType) LongType(org.hibernate.type.LongType) UUIDCharType(org.hibernate.type.UUIDCharType) Type(org.hibernate.type.Type) EntityEntry(org.hibernate.engine.spi.EntityEntry) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Collection(java.util.Collection) MarkerObject(org.hibernate.internal.util.MarkerObject) HashSet(java.util.HashSet) TypedValue(org.hibernate.engine.spi.TypedValue)

Aggregations

IdentitySet (org.hibernate.internal.util.collections.IdentitySet)6 ArrayList (java.util.ArrayList)3 List (java.util.List)2 EntityEntry (org.hibernate.engine.spi.EntityEntry)2 QueryParameters (org.hibernate.engine.spi.QueryParameters)2 RowSelection (org.hibernate.engine.spi.RowSelection)2 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)1 NonNullableTransientDependencies (org.hibernate.engine.internal.NonNullableTransientDependencies)1 EntityGraphQueryHint (org.hibernate.engine.query.spi.EntityGraphQueryHint)1 TypedValue (org.hibernate.engine.spi.TypedValue)1 QueryNode (org.hibernate.hql.internal.ast.tree.QueryNode)1 QueryTranslator (org.hibernate.hql.spi.QueryTranslator)1 MarkerObject (org.hibernate.internal.util.MarkerObject)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 CompositeType (org.hibernate.type.CompositeType)1 IntegerType (org.hibernate.type.IntegerType)1