Description
odmg ordering problem when using circular/bidirectional 1:1 references, in this case the ordering of objects doesn't change at commit.
Assume we have A-1:1->AA-1:1->AAA-1:1->A and the user persist the 'A' instance, then the expected order is
[AAA, AA, A]
because start is A (specified by the user) and the last new object of the circuit is AAA and for 1:1 references OJB first persist the reference. The PB-api show this ordering, odmg show [A, AA, AAA] in this case.
The problem is the weight of the vertices in loops or circuits of 1:1 references, because all vertices have the same weight, so the odering doesn't change.
workaround
Influence ordering by using TransactionExt.flush or make persist calls in the correct order for all objects of the circuit
Example1, loop:
tx.begin();
Shop s1 = new Shop(name + "_1");
// when using flush() we can use the "natural" order
// without getting DB constraint violence.
database.makePersistent(s1);
// write to DB object without references
tx.flush();
ShopDetail sd = new ShopDetail(name + "_1");
// now set references
s1.setDetail(sd);
sd.setShop(s1);
// no need to persist the ShopDetail object
// (will be detected by OJB)
// but it doesn't matter if you do
// database.makePersistent(sd);
tx.commit();
Example2, loop:
tx.begin();
Shop s1 = new Shop(name + "_1");
ShopDetail sd = new ShopDetail(name + "_1");
s1.setDetail(sd);
sd.setShop(s1);
// madatory to persist referenced ShopDetail first, the Shop
// object will be detected automatic. In this case first the ShopDetail
// will be created and then the Shop
database.makePersistent(sd);
database.makePersistent(s1);
tx.commit();