@Test
  @SuppressWarnings({"unchecked"})
  public void testIterateWithEvictBottomOfLoop() {
    Session s = openSession();
    s.beginTransaction();
    Set parents = new HashSet();
    for (int i = 0; i < 5; i++) {
      Parent p = new Parent(String.valueOf(i + 100));
      Child child = new Child("child" + i);
      child.setParent(p);
      p.getChildren().add(child);
      s.save(p);
      parents.add(p);
    }
    s.getTransaction().commit();
    s.close();

    s = openSession();
    s.beginTransaction();
    for (Iterator it = s.createQuery("from Parent").iterate(); it.hasNext(); ) {
      Parent p = (Parent) it.next();
      assertEquals(1, p.getChildren().size());
      s.evict(p);
    }
    s.getTransaction().commit();
    s.close();

    s = openSession();
    s.beginTransaction();
    for (Object parent : parents) {
      s.delete(parent);
    }
    s.getTransaction().commit();
    s.close();
  }
Exemplo n.º 2
0
 public void testManyToOneGeneratedIds() {
   // NOTES: Child defines a many-to-one back to its Parent.  This
   // association does not define persist cascading (which is natural;
   // a child should not be able to create its parent).
   try {
     Session s = openSession();
     s.beginTransaction();
     Parent p = new Parent("parent");
     Child c = new Child("child");
     c.setParent(p);
     s.persist(c);
     try {
       s.getTransaction().commit();
       fail("expecting TransientObjectException on flush");
     } catch (TransientObjectException e) {
       // expected result
       log.trace("handled expected exception", e);
       s.getTransaction().rollback();
     } finally {
       s.close();
     }
   } finally {
     cleanupData();
   }
 }
  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 set on parent has now been replaced with a PersistentSet...
    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();
  }
Exemplo n.º 4
0
 public void addChild(Child child) {
   getChildren().add(child);
   child.setParent(this);
 }
Exemplo n.º 5
0
 public void addChild(final Child c) {
   if (c != null) {
     c.setParent(this);
     getChildern().add(c);
   }
 }