Search in sources :

Example 31 with PersistentCollection

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

the class AbstractCollectionEventTest method testUpdateParentOneChildToNoneByRemove.

@Test
public void testUpdateParentOneChildToNoneByRemove() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    assertEquals(1, parent.getChildren().size());
    Child child = (Child) parent.getChildren().iterator().next();
    listeners.clear();
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    if (child instanceof Entity) {
        child = (Child) s.get(child.getClass(), ((Entity) child).getId());
    }
    parent.removeChild(child);
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) parent.getChildren()).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, index++);
    }
    if (child instanceof ChildWithBidirectionalManyToMany) {
        ChildWithBidirectionalManyToMany childWithManyToMany = (ChildWithBidirectionalManyToMany) child;
        if (((PersistentCollection) childWithManyToMany.getParents()).wasInitialized()) {
            checkResult(listeners, listeners.getInitializeCollectionListener(), childWithManyToMany, index++);
        }
    }
    checkResult(listeners, listeners.getPreCollectionUpdateListener(), parent, index++);
    checkResult(listeners, listeners.getPostCollectionUpdateListener(), parent, index++);
    if (child instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) child, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) child, index++);
    }
    checkNumberOfResults(listeners, index);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Transaction(org.hibernate.Transaction) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) Session(org.hibernate.Session) Test(org.junit.Test)

Example 32 with PersistentCollection

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

the class AbstractCollectionEventTest method testUpdateParentNoneToOneChild.

@Test
public void testUpdateParentNoneToOneChild() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithNoChildren("parent");
    listeners.clear();
    assertEquals(0, parent.getChildren().size());
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    Child newChild = parent.addChild("new");
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) parent.getChildren()).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, index++);
    }
    checkResult(listeners, listeners.getPreCollectionUpdateListener(), parent, index++);
    checkResult(listeners, listeners.getPostCollectionUpdateListener(), parent, index++);
    if (newChild instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getPreCollectionRecreateListener(), (ChildWithBidirectionalManyToMany) newChild, index++);
        checkResult(listeners, listeners.getPostCollectionRecreateListener(), (ChildWithBidirectionalManyToMany) newChild, index++);
    }
    checkNumberOfResults(listeners, index);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Transaction(org.hibernate.Transaction) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) Session(org.hibernate.Session) Test(org.junit.Test)

Example 33 with PersistentCollection

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

the class AbstractCollectionEventTest method testUpdateParentOneChildDiffCollectionDiffChild.

@Test
public void testUpdateParentOneChildDiffCollectionDiffChild() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    Child oldChild = (Child) parent.getChildren().iterator().next();
    listeners.clear();
    assertEquals(1, parent.getChildren().size());
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    if (oldChild instanceof Entity) {
        oldChild = (Child) s.get(oldChild.getClass(), ((Entity) oldChild).getId());
    }
    Collection oldCollection = parent.getChildren();
    parent.newChildren(createCollection());
    Child newChild = parent.addChild("new1");
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) oldCollection).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, oldCollection, index++);
    }
    if (oldChild instanceof ChildWithBidirectionalManyToMany) {
        ChildWithBidirectionalManyToMany oldChildWithManyToMany = (ChildWithBidirectionalManyToMany) oldChild;
        if (((PersistentCollection) oldChildWithManyToMany.getParents()).wasInitialized()) {
            checkResult(listeners, listeners.getInitializeCollectionListener(), oldChildWithManyToMany, index++);
        }
    }
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), parent, oldCollection, index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), parent, oldCollection, index++);
    if (oldChild instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) oldChild, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) oldChild, index++);
        checkResult(listeners, listeners.getPreCollectionRecreateListener(), (ChildWithBidirectionalManyToMany) newChild, index++);
        checkResult(listeners, listeners.getPostCollectionRecreateListener(), (ChildWithBidirectionalManyToMany) newChild, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRecreateListener(), parent, index++);
    checkResult(listeners, listeners.getPostCollectionRecreateListener(), parent, index++);
    checkNumberOfResults(listeners, index);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Transaction(org.hibernate.Transaction) Collection(java.util.Collection) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) Session(org.hibernate.Session) Test(org.junit.Test)

Example 34 with PersistentCollection

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

the class AbstractCollectionEventTest method testMoveCollectionToDifferentParentFlushMoveToDifferentParent.

@Test
public void testMoveCollectionToDifferentParentFlushMoveToDifferentParent() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    ParentWithCollection otherParent = createParentWithOneChild("otherParent", "otherChild");
    ParentWithCollection otherOtherParent = createParentWithNoChildren("otherParent");
    listeners.clear();
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    otherParent = (ParentWithCollection) s.get(otherParent.getClass(), otherParent.getId());
    otherOtherParent = (ParentWithCollection) s.get(otherOtherParent.getClass(), otherOtherParent.getId());
    Collection otherCollectionOrig = otherParent.getChildren();
    Collection otherOtherCollectionOrig = otherOtherParent.getChildren();
    otherParent.newChildren(parent.getChildren());
    parent.newChildren(null);
    s.flush();
    otherOtherParent.newChildren(otherParent.getChildren());
    otherParent.newChildren(null);
    tx.commit();
    s.close();
    int index = 0;
    Child otherChildOrig = null;
    if (((PersistentCollection) otherCollectionOrig).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), otherParent, otherCollectionOrig, index++);
        otherChildOrig = (Child) otherCollectionOrig.iterator().next();
        if (otherChildOrig instanceof ChildWithBidirectionalManyToMany) {
            checkResult(listeners, listeners.getInitializeCollectionListener(), (ChildWithBidirectionalManyToMany) otherChildOrig, index++);
        }
    }
    checkResult(listeners, listeners.getInitializeCollectionListener(), parent, otherOtherParent.getChildren(), index++);
    Child otherOtherChild = (Child) otherOtherParent.getChildren().iterator().next();
    if (otherOtherChild instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), (ChildWithBidirectionalManyToMany) otherOtherChild, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), parent, otherOtherParent.getChildren(), index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), parent, otherOtherParent.getChildren(), index++);
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), otherParent, otherCollectionOrig, index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), otherParent, otherCollectionOrig, index++);
    if (otherOtherChild instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherChildOrig, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherChildOrig, index++);
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherOtherChild, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherOtherChild, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRecreateListener(), otherParent, otherOtherParent.getChildren(), index++);
    checkResult(listeners, listeners.getPostCollectionRecreateListener(), otherParent, otherOtherParent.getChildren(), index++);
    if (((PersistentCollection) otherOtherCollectionOrig).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), otherOtherParent, otherOtherCollectionOrig, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), otherParent, otherOtherParent.getChildren(), index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), otherParent, otherOtherParent.getChildren(), index++);
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), otherOtherParent, otherOtherCollectionOrig, index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), otherOtherParent, otherOtherCollectionOrig, index++);
    if (otherOtherChild instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherOtherChild, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherOtherChild, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRecreateListener(), otherOtherParent, index++);
    checkResult(listeners, listeners.getPostCollectionRecreateListener(), otherOtherParent, index++);
    // there should also be pre- and post-recreate collection events for parent, and otherParent
    // but thats broken now; this is covered in BrokenCollectionEventTest
    checkNumberOfResults(listeners, index);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Transaction(org.hibernate.Transaction) Collection(java.util.Collection) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) Session(org.hibernate.Session) Test(org.junit.Test)

Example 35 with PersistentCollection

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

the class AbstractCollectionEventTest method testMoveCollectionToDifferentParent.

@Test
public void testMoveCollectionToDifferentParent() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    ParentWithCollection otherParent = createParentWithOneChild("otherParent", "otherChild");
    listeners.clear();
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    otherParent = (ParentWithCollection) s.get(otherParent.getClass(), otherParent.getId());
    Collection otherCollectionOrig = otherParent.getChildren();
    otherParent.newChildren(parent.getChildren());
    parent.newChildren(null);
    tx.commit();
    s.close();
    int index = 0;
    Child otherChildOrig = null;
    if (((PersistentCollection) otherCollectionOrig).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), otherParent, otherCollectionOrig, index++);
        otherChildOrig = (Child) otherCollectionOrig.iterator().next();
        if (otherChildOrig instanceof ChildWithBidirectionalManyToMany) {
            checkResult(listeners, listeners.getInitializeCollectionListener(), (ChildWithBidirectionalManyToMany) otherChildOrig, index++);
        }
    }
    checkResult(listeners, listeners.getInitializeCollectionListener(), parent, otherParent.getChildren(), index++);
    Child otherChild = (Child) otherParent.getChildren().iterator().next();
    if (otherChild instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), (ChildWithBidirectionalManyToMany) otherChild, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), parent, otherParent.getChildren(), index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), parent, otherParent.getChildren(), index++);
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), otherParent, otherCollectionOrig, index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), otherParent, otherCollectionOrig, index++);
    if (otherChild instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherChildOrig, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherChildOrig, index++);
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherChild, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) otherChild, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRecreateListener(), otherParent, index++);
    checkResult(listeners, listeners.getPostCollectionRecreateListener(), otherParent, index++);
    // there should also be pre- and post-recreate collection events for parent, but thats broken now;
    // this is covered in BrokenCollectionEventTest
    checkNumberOfResults(listeners, index);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Transaction(org.hibernate.Transaction) Collection(java.util.Collection) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) Session(org.hibernate.Session) Test(org.junit.Test)

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