props.put("openjpa.jdbc.QuerySQLCache", "true");
OpenJPAEntityManagerFactorySPI emf1 = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.
cast(Persistence.createEntityManagerFactory("test", props));
try {
EntityManagerImpl em = (EntityManagerImpl)emf1.createEntityManager();
em.getTransaction().begin();
for (int i = 1; i < 3; i++) {
TblParent p = new TblParent();
p.setParentId(i);
TblChild c = new TblChild();
c.setChildId(i);
c.setTblParent(p);
p.addTblChild(c);
em.persist(p);
em.persist(c);
TblGrandChild gc = new TblGrandChild();
gc.setGrandChildId(i);
gc.setTblChild(c);
c.addTblGrandChild(gc);
em.persist(p);
em.persist(c);
em.persist(gc);
}
em.flush();
em.getTransaction().commit();
em.clear();
for (int i = 1; i < 3; i++) {
TblParent p = em.find(TblParent.class, i);
int pid = p.getParentId();
assertEquals(pid, i);
Collection<TblChild> children = p.getTblChildren();
boolean hasChild = false;
for (TblChild c : children) {
hasChild = true;
Collection<TblGrandChild> gchildren = c.getTblGrandChildren();
int cid = c.getChildId();
assertEquals(cid, i);
boolean hasGrandChild = false;
for (TblGrandChild gc : gchildren) {
hasGrandChild = true;
int gcId = gc.getGrandChildId();
assertEquals(gcId, i);
}
assertTrue(hasGrandChild);
}
assertTrue(hasChild);
em.close();
}
} finally {
closeEMF(emf1);
}
}