There have been several posts asking how to support recursive composition in
Castor. While it is not directly supported, there is a workaround if the
class that is recursively composed is part of another class:
class A {
private Vector _bs = new Vector(); // Top-level Bs
private Vector _allBs = new Vector(); // Workaround
public void addB(B b) { _bs.add(b) }
public void addAllB(B b) { _allBs.add(b) } // Workaround
public Collection getBs() { return _bs }
}
class B {
private A _a;
private B _parent;
private Vector _subBs = new Vector();
...
public void addSubB(B b) {
_subBs.add(b);
_a.addAllB(b); // Workaround
}
public void getSubBs() { return _subBs }
}
in mapping.xml:
<class name="A" ...>
<field name="allBs" type="com.ncs.csa.projdef.Objective"
collection="collection">
<sql many-key="B_TBL_A_ID">
</field>
...
<class name="B" ...>
<field name="parent" type="com.ncs.csa.projdef.Objective">
<sql name="B_TBL_PARENT_ID">
</field>
<field name="subBs" type="com.ncs.csa.projdef.Objective"
collection="collection">
<sql many-key="B_TBL_PARENT_ID">
</field>
When the Bs are added to the 'all' collection of A, and the collection is
persisted, the recursive composition hierarchy is maintained.
Note this works for Castor JDO (create, load, update [long,short trans.],
and remove).
For Castor XML, one needs to clean up the hierarchy after unmarshalling, and
this only works if the subB collections are added as a whole instead of
using the mutator method (addSubB()), because lower-level b's have a null _a
field when the mutator is called and _a.addAllB(b) fails.
I haven't tried XML marshalling.
-doug
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev