Transaction tx = pm.currentTransaction();
try {
tx.setNontransactionalRead(true);
tx.begin();
Company c = new Company(1L, "MyCompany", new Date(), null);
Department d = new Department(999, "MyDepartment", c);
pm.makePersistent(c);
pm.makePersistent(d);
Object oid = pm.getObjectId(d);
if (!tx.getNontransactionalRead()) {
fail(ASSERTION_FAILED,
"tx.getNontransactionalRead returns false after setting the flag to true.");
}
tx.commit();
if (!tx.getNontransactionalRead()) {
fail(ASSERTION_FAILED,
"tx.getNontransactionalRead returns false after setting the flag to true.");
}
// make sure transaction is not active
if (tx.isActive()) {
fail(ASSERTION_FAILED,
"transaction still active after tx.commit.");
}
tx = null;
// read department
d = (Department)pm.getObjectById(oid, true);
long deptid = d.getDeptid();
if (deptid != 999) {
fail("Reading department outside of a transaction returns unexpected value of d.deptid, expected 999, got " + deptid);
}
// navigate from department to company
c = (Company)d.getCompany();
if (c == null) {
fail("Navigating from department to company outside of a transaction returns null.");
}
String companyName = c.getName();
if (!"MyCompany".equals(companyName)) {
fail("Navigated company returns unexpected value of c.name, expected MyCompany, got " + companyName);
}
// run query
Query q = pm.newQuery(Department.class);
q.setFilter("name == \"MyDepartment\"");
Collection result = (Collection)q.execute();
Iterator i = result.iterator();
if (!i.hasNext()) {
fail(ASSERTION_FAILED,
"Query outside of a transaction returned empty collection.");
}
d = (Department)i.next();
String deptName = d.getName();
if (!"MyDepartment".equals(deptName)) {
fail("Department in query result returns unexpected value of d.name, expected MyDepartment, got " + deptName);
}
if (i.hasNext()) {
fail(ASSERTION_FAILED,