Search in sources :

Example 1 with PersistentMap

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

the class PersistentMapTest method testWriteMethodDirtying.

@Test
@SuppressWarnings({ "unchecked" })
public void testWriteMethodDirtying() {
    Parent parent = new Parent("p1");
    Child child = new Child("c1");
    parent.getChildren().put(child.getName(), child);
    child.setParent(parent);
    Child otherChild = new Child("c2");
    Session session = openSession();
    session.beginTransaction();
    session.save(parent);
    session.flush();
    // at this point, the map on parent has now been replaced with a PersistentMap...
    PersistentMap children = (PersistentMap) parent.getChildren();
    Object old = children.put(child.getName(), child);
    assertTrue(old == child);
    assertFalse(children.isDirty());
    old = children.remove(otherChild.getName());
    assertNull(old);
    assertFalse(children.isDirty());
    HashMap otherMap = new HashMap();
    otherMap.put(child.getName(), child);
    children.putAll(otherMap);
    assertFalse(children.isDirty());
    otherMap = new HashMap();
    otherMap.put(otherChild.getName(), otherChild);
    children.putAll(otherMap);
    assertTrue(children.isDirty());
    children.clearDirty();
    session.delete(child);
    children.clear();
    assertTrue(children.isDirty());
    session.flush();
    children.clear();
    assertFalse(children.isDirty());
    session.delete(parent);
    session.getTransaction().commit();
    session.close();
}
Also used : PersistentMap(org.hibernate.collection.internal.PersistentMap) HashMap(java.util.HashMap) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)1 Session (org.hibernate.Session)1 PersistentMap (org.hibernate.collection.internal.PersistentMap)1 Test (org.junit.Test)1