Search in sources :

Example 1 with CollectionListeners

use of org.hibernate.test.event.collection.CollectionListeners in project hibernate-orm by hibernate.

the class AbstractAssociationCollectionEventTest method testDeleteParentButNotChild.

@Test
public void testDeleteParentButNotChild() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    ChildEntity child = (ChildEntity) parent.getChildren().iterator().next();
    listeners.clear();
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    child = (ChildEntity) s.get(child.getClass(), child.getId());
    parent.removeChild(child);
    s.delete(parent);
    tx.commit();
    s.close();
    int index = 0;
    checkResult(listeners, listeners.getInitializeCollectionListener(), parent, index++);
    if (child instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), (ChildWithBidirectionalManyToMany) child, index++);
    }
    checkResult(listeners, listeners.getPreCollectionRemoveListener(), parent, index++);
    checkResult(listeners, listeners.getPostCollectionRemoveListener(), 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 : CollectionListeners(org.hibernate.test.event.collection.CollectionListeners) Transaction(org.hibernate.Transaction) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) ChildEntity(org.hibernate.test.event.collection.ChildEntity) ParentWithCollection(org.hibernate.test.event.collection.ParentWithCollection) Session(org.hibernate.Session) Test(org.junit.Test) AbstractCollectionEventTest(org.hibernate.test.event.collection.AbstractCollectionEventTest)

Aggregations

Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)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 ChildWithBidirectionalManyToMany (org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany)1 Test (org.junit.Test)1