use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ManyOneTest method testRemoveChildReferenceDetached.
public void testRemoveChildReferenceDetached() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
pm.getFetchPlan().addGroup(FetchPlan.ALL);
tx.begin();
Department engineering = pm.getObjectById(Department.class, "Engineering");
Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
assertEquals(2, engineering.getPersons().size());
pm.getFetchPlan().setMaxFetchDepth(-1);
Person detachedBugsBunny = pm.detachCopy(bugsBunny);
Department detachedEngineering = pm.detachCopy(engineering);
tx.commit();
pm.close();
// remove a child reference
detachedEngineering.getPersons().remove(detachedBugsBunny);
// detachedBugsBunny.setDepartment(null);
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
pm.makePersistent(detachedEngineering);
tx.commit();
pm.close();
// now check the removed child 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());
// check object doesn't exist
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 testUpdateField.
public void testUpdateField() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person bbunny = pm.getObjectById(Person.class, "Bugs Bunny");
bbunny.setFirstName("BBB");
tx.commit();
pm.close();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
bbunny = pm.getObjectById(Person.class, "Bugs Bunny");
assertEquals("BBB", bbunny.getFirstName());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ManyOneTest method testSwapParentReference.
public void testSwapParentReference() {
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");
Person lamiPuxa = pm.getObjectById(Person.class, "Lami Puxa");
assertEquals(2, engineering.getPersons().size());
assertEquals(1, sales.getPersons().size());
assertTrue(engineering.getPersons().contains(bugsBunny));
assertEquals(engineering, bugsBunny.getDepartment());
assertTrue(sales.getPersons().contains(lamiPuxa));
assertEquals(sales, lamiPuxa.getDepartment());
// swap references
bugsBunny.setDepartment(sales);
lamiPuxa.setDepartment(engineering);
tx.commit();
pm.close();
// now check the swapped references
pm = pmf.getPersistenceManager();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
tx = pm.currentTransaction();
tx.begin();
engineering = pm.getObjectById(Department.class, "Engineering");
sales = pm.getObjectById(Department.class, "Sales");
bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
lamiPuxa = pm.getObjectById(Person.class, "Lami Puxa");
assertEquals(2, engineering.getPersons().size());
assertEquals(1, sales.getPersons().size());
assertTrue(engineering.getPersons().contains(lamiPuxa));
assertTrue(sales.getPersons().contains(bugsBunny));
assertEquals(engineering, lamiPuxa.getDepartment());
assertEquals(sales, bugsBunny.getDepartment());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ManyOneTest method testPersistWithNoChild.
/**
* Only the parent object is persisted, the reference to the child object is empty.
*/
public void testPersistWithNoChild() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Company c = new Company("company");
pm.makePersistent(c);
tx.commit();
helper.ids.add(pm.getObjectId(c));
pm.close();
// test
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
c = pm.getObjectById(Company.class, "company");
assertNotNull(c.getDepartments());
assertTrue(c.getDepartments().isEmpty());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ManyOneTest method testRemoveChildReference.
public void testRemoveChildReference() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
pm.getFetchPlan().addGroup(FetchPlan.ALL);
tx.begin();
Department engineering = pm.getObjectById(Department.class, "Engineering");
Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
assertEquals(2, engineering.getPersons().size());
// remove a child reference
engineering.getPersons().remove(bugsBunny);
bugsBunny.setDepartment(null);
tx.commit();
pm.close();
// now check the removed child 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());
// check object doesn't exist
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();
}
}
Aggregations