Search in sources :

Example 6 with PersistentCollection

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

the class OnReplicateVisitor method processCollection.

@Override
public Object processCollection(Object collection, CollectionType type) throws HibernateException {
    if (collection == CollectionType.UNFETCHED_COLLECTION) {
        return null;
    }
    final EventSource session = getSession();
    final CollectionPersister persister = session.getFactory().getMetamodel().collectionPersister(type.getRole());
    if (isUpdate) {
        removeCollection(persister, extractCollectionKeyFromOwner(persister), session);
    }
    if (collection != null && collection instanceof PersistentCollection) {
        final PersistentCollection wrapper = (PersistentCollection) collection;
        wrapper.setCurrentSession((SessionImplementor) session);
        if (wrapper.wasInitialized()) {
            session.getPersistenceContext().addNewCollection(persister, wrapper);
        } else {
            reattachCollection(wrapper, type);
        }
    } else {
    // otherwise a null or brand new collection
    // this will also (inefficiently) handle arrays, which
    // have no snapshot, so we can't do any better
    //processArrayOrNewCollection(collection, type);
    }
    return null;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EventSource(org.hibernate.event.spi.EventSource) CollectionPersister(org.hibernate.persister.collection.CollectionPersister)

Example 7 with PersistentCollection

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

the class OnUpdateVisitor method processCollection.

@Override
Object processCollection(Object collection, CollectionType type) throws HibernateException {
    if (collection == CollectionType.UNFETCHED_COLLECTION) {
        return null;
    }
    EventSource session = getSession();
    CollectionPersister persister = session.getFactory().getCollectionPersister(type.getRole());
    final Serializable collectionKey = extractCollectionKeyFromOwner(persister);
    if (collection != null && (collection instanceof PersistentCollection)) {
        PersistentCollection wrapper = (PersistentCollection) collection;
        if (wrapper.setCurrentSession(session)) {
            //a "detached" collection!
            if (!isOwnerUnchanged(wrapper, persister, collectionKey)) {
                // if the collection belonged to a different entity,
                // clean up the existing state of the collection
                removeCollection(persister, collectionKey, session);
            }
            reattachCollection(wrapper, type);
        } else {
            // a collection loaded in the current session
            // can not possibly be the collection belonging
            // to the entity passed to update()
            removeCollection(persister, collectionKey, session);
        }
    } else {
        // null or brand new collection
        // this will also (inefficiently) handle arrays, which have
        // no snapshot, so we can't do any better
        removeCollection(persister, collectionKey, session);
    }
    return null;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EventSource(org.hibernate.event.spi.EventSource) Serializable(java.io.Serializable) CollectionPersister(org.hibernate.persister.collection.CollectionPersister)

Example 8 with PersistentCollection

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

the class WrapVisitor method processCollection.

@Override
Object processCollection(Object collection, CollectionType collectionType) throws HibernateException {
    if (collection != null && (collection instanceof PersistentCollection)) {
        final SessionImplementor session = getSession();
        PersistentCollection coll = (PersistentCollection) collection;
        if (coll.setCurrentSession(session)) {
            reattachCollection(coll, collectionType);
        }
        return null;
    } else {
        return processArrayOrNewCollection(collection, collectionType);
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 9 with PersistentCollection

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

the class WrapVisitor method processArrayOrNewCollection.

final Object processArrayOrNewCollection(Object collection, CollectionType collectionType) throws HibernateException {
    final SessionImplementor session = getSession();
    if (collection == null) {
        //do nothing
        return null;
    } else {
        CollectionPersister persister = session.getFactory().getCollectionPersister(collectionType.getRole());
        final PersistenceContext persistenceContext = session.getPersistenceContext();
        //TODO: move into collection type, so we can use polymorphism!
        if (collectionType.hasHolder()) {
            if (collection == CollectionType.UNFETCHED_COLLECTION) {
                return null;
            }
            PersistentCollection ah = persistenceContext.getCollectionHolder(collection);
            if (ah == null) {
                ah = collectionType.wrap(session, collection);
                persistenceContext.addNewCollection(persister, ah);
                persistenceContext.addCollectionHolder(ah);
            }
            return null;
        } else {
            PersistentCollection persistentCollection = collectionType.wrap(session, collection);
            persistenceContext.addNewCollection(persister, persistentCollection);
            if (LOG.isTraceEnabled()) {
                LOG.tracev("Wrapped collection in role: {0}", collectionType.getRole());
            }
            //Force a substitution!
            return persistentCollection;
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 10 with PersistentCollection

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

the class EvictVisitor method evictCollection.

public void evictCollection(Object value, CollectionType type) {
    final Object pc;
    if (type.hasHolder()) {
        pc = getSession().getPersistenceContext().removeCollectionHolder(value);
    } else if (value instanceof PersistentCollection) {
        pc = value;
    } else {
        //EARLY EXIT!
        return;
    }
    PersistentCollection collection = (PersistentCollection) pc;
    if (collection.unsetSession(getSession())) {
        evictCollection(collection);
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection)

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