Example usage for org.hibernate.engine.internal ForeignKeys getEntityIdentifierIfNotUnsaved

List of usage examples for org.hibernate.engine.internal ForeignKeys getEntityIdentifierIfNotUnsaved

Introduction

In this page you can find the example usage for org.hibernate.engine.internal ForeignKeys getEntityIdentifierIfNotUnsaved.

Prototype

public static Serializable getEntityIdentifierIfNotUnsaved(final String entityName, final Object object,
        final SharedSessionContractImplementor session) throws TransientObjectException 

Source Link

Document

Return the identifier of the persistent or transient object, or throw an exception if the instance is "unsaved"

Used by OneToOneType and ManyToOneType to determine what id value should be used for an object that may or may not be associated with the session.

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 av 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;
}