Search in sources :

Example 1 with ChildWithBidirectionalManyToMany

use of org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testUpdateParentNullToOneChild.

@Test
public void testUpdateParentNullToOneChild() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithNullChildren("parent");
    listeners.clear();
    assertNull(parent.getChildren());
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    assertNotNull(parent.getChildren());
    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 2 with ChildWithBidirectionalManyToMany

use of org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testUpdateParentOneChildToNoneByClear.

@Test
public void testUpdateParentOneChildToNoneByClear() {
    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.clearChildren();
    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 3 with ChildWithBidirectionalManyToMany

use of org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testUpdateParentOneToTwoChildren.

@Test
public void testUpdateParentOneToTwoChildren() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    assertEquals(1, parent.getChildren().size());
    listeners.clear();
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    Child newChild = parent.addChild("new2");
    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 4 with ChildWithBidirectionalManyToMany

use of org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testMoveAllChildrenToDifferentParent.

@Test
public void testMoveAllChildrenToDifferentParent() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    ParentWithCollection otherParent = createParentWithOneChild("otherParent", "otherChild");
    Child child = (Child) parent.getChildren().iterator().next();
    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());
    if (child instanceof Entity) {
        child = (Child) s.get(child.getClass(), ((Entity) child).getId());
    }
    otherParent.addAllChildren(parent.getChildren());
    parent.clearChildren();
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) parent.getChildren()).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, index++);
    }
    if (((PersistentCollection) otherParent.getChildren()).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), otherParent, index++);
    }
    if (child instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), (ChildWithBidirectionalManyToMany) child, index++);
    }
    checkResult(listeners, listeners.getPreCollectionUpdateListener(), parent, index++);
    checkResult(listeners, listeners.getPostCollectionUpdateListener(), parent, index++);
    checkResult(listeners, listeners.getPreCollectionUpdateListener(), otherParent, index++);
    checkResult(listeners, listeners.getPostCollectionUpdateListener(), otherParent, 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 5 with ChildWithBidirectionalManyToMany

use of org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testUpdateParentNoneToOneChildDiffCollection.

@Test
public void testUpdateParentNoneToOneChildDiffCollection() {
    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());
    Collection oldCollection = parent.getChildren();
    parent.newChildren(createCollection());
    Child newChild = parent.addChild("new");
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) oldCollection).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, oldCollection, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), parent, oldCollection, index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), parent, oldCollection, index++);
    if (newChild instanceof ChildWithBidirectionalManyToMany) {
        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)

Aggregations

Session (org.hibernate.Session)17 Transaction (org.hibernate.Transaction)17 ChildWithBidirectionalManyToMany (org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany)17 Test (org.junit.Test)17 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)15 Collection (java.util.Collection)6 PersistentSet (org.hibernate.collection.internal.PersistentSet)1 AbstractCollectionEventTest (org.hibernate.test.event.collection.AbstractCollectionEventTest)1 ChildEntity (org.hibernate.test.event.collection.ChildEntity)1 CollectionListeners (org.hibernate.test.event.collection.CollectionListeners)1 ParentWithCollection (org.hibernate.test.event.collection.ParentWithCollection)1