em.close();
}
public void testSetNullEagerRelationship() {
PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
EntityManager em = emf.createEntityManager();
try {
OneToEntity ote = new OneToEntity();
assertFalse(puu.isLoaded(ote, "toManyEager"));
em.getTransaction().begin();
em.persist(ote);
em.getTransaction().commit();
em.clear();
ote = em.find(OneToEntity.class, ote.getId());
// Field is eager and is immediately loaded by the application
assertTrue(puu.isLoaded(ote, "toManyEager"));
OneToEntity ote2 = new OneToEntity();
em.getTransaction().begin();
em.persist(ote2);
// Field is null by default, but after persist, it is treated as loaded.
assertTrue(puu.isLoaded(ote2, "toManyEager"));
em.getTransaction().commit();
// Field gets set to loaded upon commit
assertTrue(puu.isLoaded(ote2, "toManyEager"));
em.clear();
ote2 = em.find(OneToEntity.class, ote2.getId());
// Field is eager and is immediately loaded by the application
assertTrue(puu.isLoaded(ote2, "toManyEager"));
// Load by application
List<ToManyEager> tmes = new ArrayList<ToManyEager>();
for (int i = 0; i < 5; i++) {
tmes.add(new ToManyEager("ToMany" + i));
}
em.getTransaction().begin();
ote2.setToManyEager(tmes);
// App loaded before commit
assertTrue(puu.isLoaded(ote2, "toManyEager"));
em.getTransaction().commit();
// Still loaded after commit
assertTrue(puu.isLoaded(ote2, "toManyEager"));
// Set to null - still loaded per spec.
em.getTransaction().begin();
ote2.setToManyEager(null);
// Entity is considered loaded before commit
assertTrue(puu.isLoaded(ote2));
// Attribute is considered loaded before commit
assertTrue(puu.isLoaded(ote2, "toManyEager"));
em.getTransaction().commit();
//Loaded after commit
assertTrue(puu.isLoaded(ote2, "toManyEager"));
}
finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}