obj2.setPk(contractPk);
obj2.setContractValue2(2);
try
{
// 1. Insert object
Transaction tx = odmg.newTransaction();
tx.begin();
/*
arminw:
seems to have problems when within a tx a object
was stored/deleted.
Without obj1 test pass
TODO: fix this
*/
//database.makePersistent(obj1);
database.makePersistent(obj2);
//database.deletePersistent(obj1);
/*
thma: I checked this, and don't see a problem here.
obj1 and obj2 have the same Identity. Thus the
calls database.makePersistent(obj1); and database.makePersistent(obj2);
will only register one instance to the transaction.
The second call does not add a second instance, but just marks the
existing instance as dirty a second time.
So it's no wonder why after deletePersistent(obj1); no contract is found.
Works as designed.
The Lesson to learn: never let business objects have the same primary key values!
* */
tx.commit();
Collection result = getContract(contractPk, odmg);
assertEquals("We should found exact one contract", 1, result.size());
obj2 = (Contract) result.iterator().next();
if (obj2 == null)
{
fail("Contract not found");
}
else if (obj2.getContractValue2() != 2)
{
fail("Wrong contract found");
}
// 2. Delete, then insert object with the same identity
tx.begin();
database.deletePersistent(obj2);
database.makePersistent(obj1);
tx.commit();
result = getContract(contractPk, odmg);
assertEquals("We should found exact one contract", 1, result.size());
obj1 = (Contract) result.iterator().next();
if (obj1 == null)
{
fail("Contract not found");
}
else if (obj1.getContractValue2() != 1)
{
fail("Wrong contract found");
}
// 3. Delete
tx.begin();
database.deletePersistent(obj1);
tx.commit();
result = getContract(contractPk, odmg);
if (result.size() > 0)
{
fail("Contract was not deleted");