use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ManyOneTest method testUpdateChildReferenceDetached.
public void testUpdateChildReferenceDetached() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
pm.getFetchPlan().addGroup(FetchPlan.ALL);
tx.begin();
Department engineering = pm.getObjectById(Department.class, "Engineering");
Department sales = pm.getObjectById(Department.class, "Sales");
Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
assertEquals(engineering, bugsBunny.getDepartment());
assertEquals(2, engineering.getPersons().size());
assertEquals(1, sales.getPersons().size());
Person detachedBugsBunny = pm.detachCopy(bugsBunny);
Department detachedSales = pm.detachCopy(sales);
Department detachedEngineering = pm.detachCopy(engineering);
tx.commit();
pm.close();
// set new child references
detachedSales.getPersons().add(detachedBugsBunny);
detachedEngineering.getPersons().remove(detachedBugsBunny);
detachedBugsBunny.setDepartment(detachedSales);
detachedBugsBunny.setFirstName("BBB");
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
tx = pm.currentTransaction();
tx.begin();
pm.makePersistent(detachedBugsBunny);
tx.commit();
pm.close();
// now check the updated reference
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
tx = pm.currentTransaction();
tx.begin();
bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
sales = pm.getObjectById(Department.class, "Sales");
engineering = pm.getObjectById(Department.class, "Engineering");
assertEquals(sales, bugsBunny.getDepartment());
assertEquals("BBB", bugsBunny.getFirstName());
assertEquals(2, sales.getPersons().size());
assertTrue(sales.getPersons().contains(bugsBunny));
assertEquals(1, engineering.getPersons().size());
assertFalse(engineering.getPersons().contains(bugsBunny));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ManyOneTest method testAddChildReferenceDetached.
public void testAddChildReferenceDetached() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
pm.getFetchPlan().addGroup("persons_of_department");
tx.begin();
Department engineering = pm.getObjectById(Department.class, "Engineering");
assertEquals(2, engineering.getPersons().size());
Department detachedEngineering = pm.detachCopy(engineering);
tx.commit();
pm.close();
// add new child references
Person dduck = new Person("Daffy", "Duck", "Daffy Duck", null, null, detachedEngineering);
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
pm.makePersistent(dduck);
tx.commit();
pm.close();
// now check the new child reference
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup("persons_of_department");
tx = pm.currentTransaction();
tx.begin();
dduck = pm.getObjectById(Person.class, "Daffy Duck");
engineering = pm.getObjectById(Department.class, "Engineering");
assertEquals(3, engineering.getPersons().size());
assertTrue(engineering.getPersons().contains(dduck));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ManyOneTest method testUpdateChildReference.
public void testUpdateChildReference() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
pm.getFetchPlan().addGroup("persons_of_department");
tx.begin();
Department engineering = pm.getObjectById(Department.class, "Engineering");
Department sales = pm.getObjectById(Department.class, "Sales");
Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
assertEquals(engineering, bugsBunny.getDepartment());
// set new child references
sales.getPersons().add(bugsBunny);
engineering.getPersons().remove(bugsBunny);
bugsBunny.setDepartment(sales);
tx.commit();
pm.close();
// now check the updated reference
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup("persons_of_department");
tx = pm.currentTransaction();
tx.begin();
bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
sales = pm.getObjectById(Department.class, "Sales");
engineering = pm.getObjectById(Department.class, "Engineering");
assertEquals(sales, bugsBunny.getDepartment());
assertTrue(sales.getPersons().contains(bugsBunny));
assertFalse(engineering.getPersons().contains(bugsBunny));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ManyOneTest method testDeleteReferenceByDeletingObject.
public void testDeleteReferenceByDeletingObject() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
pm.getFetchPlan().addGroup(FetchPlan.ALL);
tx.begin();
Department engineering = pm.getObjectById(Department.class, "Engineering");
Person bbunny = pm.getObjectById(Person.class, "Bugs Bunny");
assertEquals(2, engineering.getPersons().size());
assertTrue(engineering.getPersons().contains(bbunny));
assertEquals(engineering, bbunny.getDepartment());
// delete object
pm.deletePersistent(bbunny);
tx.commit();
pm.close();
// now check the removed reference
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
tx = pm.currentTransaction();
tx.begin();
engineering = pm.getObjectById(Department.class, "Engineering");
assertEquals(1, engineering.getPersons().size());
try {
pm.getObjectById(Person.class, "Bugs Bunny");
fail("Object 'Bugs Bunny' should not exist any more!");
} catch (JDOObjectNotFoundException e) {
// expected
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ManyOneTest method testUpdateParentReferenceDetached.
public void testUpdateParentReferenceDetached() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
pm.getFetchPlan().addGroup("persons_of_department");
tx.begin();
Department engineering = pm.getObjectById(Department.class, "Engineering");
Department sales = pm.getObjectById(Department.class, "Sales");
Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
assertEquals(engineering, bugsBunny.getDepartment());
Person detachedBugsBunny = pm.detachCopy(bugsBunny);
Department detachedSales = pm.detachCopy(sales);
tx.commit();
pm.close();
// set new parent reference
detachedBugsBunny.setDepartment(detachedSales);
detachedBugsBunny.setFirstName("BBB");
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
pm.makePersistent(detachedBugsBunny);
tx.commit();
pm.close();
// now check the updated reference
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup("persons_of_department");
tx = pm.currentTransaction();
tx.begin();
bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
sales = pm.getObjectById(Department.class, "Sales");
engineering = pm.getObjectById(Department.class, "Engineering");
assertEquals(sales, bugsBunny.getDepartment());
assertEquals("BBB", bugsBunny.getFirstName());
assertTrue(sales.getPersons().contains(bugsBunny));
assertFalse(engineering.getPersons().contains(bugsBunny));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations