The power of a Java persistence solution like DataNucleus is demonstrated when persisting relationships between objects. There are many types of relationships.
When the relation is unidirectional you simply set the related field to refer to the other object. For example we have classes A and B and the class A has a field of type B. So we set it like this
A a = new A(); B b = new B(); a.setB(b); // "a" knows about "b"
When the relation is bidirectional you have to set both sides of the relation. For example, we have classes A and B and the class A has a collection of elements of type B, and B has a field of type A. So we set it like this
A a = new A(); B b1 = new B(); a.addElement(b1); // "a" knows about "b1" b1.setA(a); // "b1" knows about "a"
So it is really simple, with only 1 general rule. With a bidirectional relation you should set both sides of the relation
With JDO, when you persist an object, all related objects (reachable from the fields of the object being persisted) will be persisted at the same time (unless already persistent). This is called persistence-by-reachability. This additionally applies when you have an object managed by the PersistenceManager, and you set a field to refer to a related object - this will make the related object provisionally persistent also.
One additional feature of JDO is the ability to re-run the persistence-by-reachability algorithm at commit so as to check whether the objects being made persistent should definitely be persisted. This is for the following situation.
As previously mentioned, users should really set both sides of a bidirectional relation. DataNucleus provides a good level of managed relations in that it will attempt to correct any missing information in relations to make both sides consistent. This is defined below
For a 1-1 bidirectional relation, at persist you should set one side of the relation and the other side will be set to make it consistent. If the respective sides are set to inconsistent objects then an exception will be thrown at persist. At update of owner/non-owner side the other side will also be updated to make them consistent.
For a 1-N bidirectional relation and you only specify the element owner then the collection must be Set-based since DataNucleus cannot generate indexing information for you in that situation (you must position the elements). At update of element or owner the other side will also be updated to make them consistent. At delete of element the owner collection will also be updated to make them consistent. If you are using a List you MUST set both sides of the relation
For an M-N bidirectional relation, at persist you MUST set the one side and the other side will be populated at commit/flush to make them consistent.
This management of relations can be turned on/off using a PMF property datanucleus.manageRelationships. If you always set both sides of a relation at persist or update then you could safely turn it off.
|When performing management of relations there are some checks implemented to spot typical errors in user operations e.g add an element to a collection and then remove it (why?!). You can disable these checks using datanucleus.manageRelationshipsChecks, set to false.|