}
public void testSerializeWithOneToMany_AddGrandchildToUnidirectionalDetached() throws Exception {
beginTxn();
HasGrandchildJPA hasGrandchild = new HasGrandchildJPA();
HasOneToManySetJPA pojo = new HasOneToManySetJPA();
hasGrandchild.getYar().add(pojo);
pojo.setVal("yar");
Book b = new Book();
b.setAuthor("harry");
pojo.getBooks().add(b);
em.persist(hasGrandchild);
commitTxn();
em.close();
em = emf.createEntityManager();
hasGrandchild = toBytesAndBack(hasGrandchild);
pojo = hasGrandchild.getYar().iterator().next();
assertEquals("yar", pojo.getVal());
assertEquals(1, pojo.getBooks().size());
Book b2 = new Book();
b2.setAuthor("yar3");
pojo.getBooks().add(b2);
// Don't set the parent - this ref won't get updated when we call
// merge and we'll get an exception.
// bidir2.setParent(pojo);
beginTxn();
hasGrandchild = em.merge(hasGrandchild);
commitTxn();
Entity e = ds.get(KeyFactory.stringToKey(b2.getId()));
assertEquals(KeyFactory.stringToKey(pojo.getId()), e.getKey().getParent());
}