Example usage for org.hibernate.collection.spi PersistentCollection unsetSession

List of usage examples for org.hibernate.collection.spi PersistentCollection unsetSession

Introduction

In this page you can find the example usage for org.hibernate.collection.spi PersistentCollection unsetSession.

Prototype

boolean unsetSession(SharedSessionContractImplementor currentSession);

Source Link

Document

Disassociate this collection from the given session.

Usage

From source file:org.jspresso.framework.application.backend.persistence.hibernate.HibernateHelper.java

License:Open Source License

/**
 * Whenever the entity has dirty persistent collection, make them clean to
 * workaround a "bug" with hibernate since hibernate cannot re-attach a
 * "dirty" detached collection./*from   w ww .  j a v  a 2  s.  co  m*/
 *
 * @param collection    the collection
 * @param targetSession the session that is targeted to after the dirty states have been
 *          reset or null if none.
 */
public static void unsetCollectionHibernateSession(PersistentCollection collection, Session targetSession) {
    // Whenever the entity has dirty persistent collection, make them
    // clean to workaround a "bug" with hibernate since hibernate cannot
    // re-attach a "dirty" detached collection.
    if (collection != null) {
        if (Hibernate.isInitialized(collection)) {
            collection.clearDirty();
        }
        if (collection instanceof AbstractPersistentCollection
                && ((AbstractPersistentCollection) collection).getSession() != null
                && targetSession != ((AbstractPersistentCollection) collection).getSession()) {
            // The following is to avoid to avoid Hibernate exceptions due to
            // re-associating a collection that is already associated with the
            // session.
            collection.unsetSession(((AbstractPersistentCollection) collection).getSession());
        }
    }
}