Example usage for org.hibernate.engine.spi Status SAVING

List of usage examples for org.hibernate.engine.spi Status SAVING

Introduction

In this page you can find the example usage for org.hibernate.engine.spi Status SAVING.

Prototype

Status SAVING

To view the source code for org.hibernate.engine.spi Status SAVING.

Click Source Link

Usage

From source file:org.babyfish.hibernate.collection.spi.persistence.AbstractBasePersistence.java

License:Open Source License

protected static <E> Collection<E> getOrphans(XCollection<E> oldElements, XCollection<E> currentElements,
        String entityName, SessionImplementor session) throws HibernateException {

    // short-circuit(s)
    if (currentElements.isEmpty()) {
        return oldElements; // no new elements, the old list contains only Orphans
    }//w w w  . j  a v  a 2  s  .  c o m
    if (oldElements.size() == 0) {
        return oldElements; // no old elements, so no Orphans neither
    }

    final EntityPersister entityPersister = session.getFactory().getEntityPersister(entityName);
    final Type idType = entityPersister.getIdentifierType();

    // create the collection holding the Orphans
    Collection<E> res = new ArrayList<E>(oldElements.unifiedComparator());

    // collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access
    Set<Serializable> currentIds = new HashSet<Serializable>();
    Set<E> currentSaving = new HashSet<E>(ReferenceEqualityComparator.getInstance());
    for (E current : currentElements) {
        if (current != null && ForeignKeys.isNotTransient(entityName, current, null, session)) {
            EntityEntry ee = session.getPersistenceContext().getEntry(current);
            if (ee != null && ee.getStatus() == Status.SAVING) {
                currentSaving.add(current);
            } else {
                Serializable currentId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, current,
                        session);
                currentIds.add(new TypedValue(idType, currentId));
            }
        }
    }

    // iterate over the *old* list
    for (E old : oldElements) {
        if (!currentSaving.contains(old)) {
            Serializable oldId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, old, session);
            if (!currentIds.contains(new TypedValue(idType, oldId))) {
                res.add(old);
            }
        }
    }

    return res;
}