props.put("openjpa.jdbc.QuerySQLCache", "true");
OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI)
OpenJPAPersistence.cast(
Persistence.createEntityManagerFactory("test", props));
EntityManagerImpl em = (EntityManagerImpl)emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("Delete from TblGrandChild").executeUpdate();
em.createQuery("Delete from TblChild").executeUpdate();
em.createQuery("Delete from TblParent").executeUpdate();
em.getTransaction().commit();
em.close();
em = (EntityManagerImpl) emf.createEntityManager();
em.getTransaction().begin();
for (int i = 0; i < 2; 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 = 0; i < 2; 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();
emf.close();
}