Search in sources :

Example 1 with PersistentSet

use of org.hibernate.collection.internal.PersistentSet in project hibernate-orm by hibernate.

the class PersistentSetTest method testCompositeElementWriteMethodDirtying.

@Test
public void testCompositeElementWriteMethodDirtying() {
    Container container = new Container("p1");
    Container.Content c1 = new Container.Content("c1");
    container.getContents().add(c1);
    Container.Content c2 = new Container.Content("c2");
    Session session = openSession();
    session.beginTransaction();
    session.save(container);
    session.flush();
    // at this point, the set on container has now been replaced with a PersistentSet...
    PersistentSet children = (PersistentSet) container.getContents();
    assertFalse(children.add(c1));
    assertFalse(children.isDirty());
    assertFalse(children.remove(c2));
    assertFalse(children.isDirty());
    HashSet otherSet = new HashSet();
    otherSet.add(c1);
    assertFalse(children.addAll(otherSet));
    assertFalse(children.isDirty());
    assertFalse(children.retainAll(otherSet));
    assertFalse(children.isDirty());
    otherSet = new HashSet();
    otherSet.add(c2);
    assertFalse(children.removeAll(otherSet));
    assertFalse(children.isDirty());
    assertTrue(children.retainAll(otherSet));
    assertTrue(children.isDirty());
    assertTrue(children.isEmpty());
    children.clear();
    assertTrue(children.isDirty());
    session.flush();
    children.clear();
    assertFalse(children.isDirty());
    session.delete(container);
    session.getTransaction().commit();
    session.close();
}
Also used : PersistentSet(org.hibernate.collection.internal.PersistentSet) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with PersistentSet

use of org.hibernate.collection.internal.PersistentSet in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testUpdateParentOneToTwoSameChildren.

@Test
public void testUpdateParentOneToTwoSameChildren() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    Child child = (Child) parent.getChildren().iterator().next();
    assertEquals(1, parent.getChildren().size());
    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.addChild(child);
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) parent.getChildren()).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, index++);
    }
    ChildWithBidirectionalManyToMany childWithManyToMany = null;
    if (child instanceof ChildWithBidirectionalManyToMany) {
        childWithManyToMany = (ChildWithBidirectionalManyToMany) child;
        if (((PersistentCollection) childWithManyToMany.getParents()).wasInitialized()) {
            checkResult(listeners, listeners.getInitializeCollectionListener(), childWithManyToMany, index++);
        }
    }
    if (!(parent.getChildren() instanceof PersistentSet)) {
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), parent, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), parent, index++);
    }
    if (childWithManyToMany != null && !(childWithManyToMany.getParents() instanceof PersistentSet)) {
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), childWithManyToMany, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), childWithManyToMany, 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) PersistentSet(org.hibernate.collection.internal.PersistentSet) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with PersistentSet

use of org.hibernate.collection.internal.PersistentSet in project hibernate-orm by hibernate.

the class PersistentSetTest method testWriteMethodDirtying.

@Test
public void testWriteMethodDirtying() {
    Parent parent = new Parent("p1");
    Child child = new Child("c1");
    parent.getChildren().add(child);
    child.setParent(parent);
    Child otherChild = new Child("c2");
    Session session = openSession();
    session.beginTransaction();
    session.save(parent);
    session.flush();
    // at this point, the set on parent has now been replaced with a PersistentSet...
    PersistentSet children = (PersistentSet) parent.getChildren();
    assertFalse(children.add(child));
    assertFalse(children.isDirty());
    assertFalse(children.remove(otherChild));
    assertFalse(children.isDirty());
    HashSet otherSet = new HashSet();
    otherSet.add(child);
    assertFalse(children.addAll(otherSet));
    assertFalse(children.isDirty());
    assertFalse(children.retainAll(otherSet));
    assertFalse(children.isDirty());
    otherSet = new HashSet();
    otherSet.add(otherChild);
    assertFalse(children.removeAll(otherSet));
    assertFalse(children.isDirty());
    assertTrue(children.retainAll(otherSet));
    assertTrue(children.isDirty());
    assertTrue(children.isEmpty());
    children.clear();
    session.delete(child);
    assertTrue(children.isDirty());
    session.flush();
    children.clear();
    assertFalse(children.isDirty());
    session.delete(parent);
    session.getTransaction().commit();
    session.close();
}
Also used : PersistentSet(org.hibernate.collection.internal.PersistentSet) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)3 PersistentSet (org.hibernate.collection.internal.PersistentSet)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 Transaction (org.hibernate.Transaction)1 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)1 ChildWithBidirectionalManyToMany (org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany)1