I've got a problem with using Castor XML and objects which have a bidirectional
relationship to each other via Java references. As a test example, I have a
class Paper, which contains many sortable Dataset objects (sorry about the
length of this):
public class Paper {
private SortedSet<Dataset> datasets = new TreeSet<Dataset>();
[...]
/** Get datasets. */
public SortedSet<Dataset> getDatasets() {
return datasets;
}
/** Set datasets explicitly from a SortedSet, to keep Hibernate happy. */
public Paper setDatasets(SortedSet<Dataset> datasets) {
log().warn("Setting datasets from a SortedSet");
this.datasets = datasets;
return this;
}
/** Set datasets from a generic Collection. */
public Paper setDatasets(Collection<Dataset> datasets) {
log().warn("Setting datasets from a Collection");
setDatasets(new TreeSet(datasets));
return this;
}
/** Set datasets from an array. */
public Paper setDatasets(Dataset[] datasets) {
log().warn("Setting datasets from an array");
setDatasets(Arrays.asList(datasets));
return this;
}
/** Add a single dataset. */
public Paper addDataset(Dataset dataset) {
log().warn("Adding a dataset");
if (dataset != null) {
dataset.setPaper(this); /// BIDIRECTIONAL
this.getDatasets().add(dataset);
} else {
log().warn("Tried to add null dataset to a paper");
}
return this;
}
}
and Dataset is defined as
public class Dataset {
private Paper paper;
public Dataset setPaper(Paper p) {
if (paper != null) {
paper.getDatasets().add(this); /// BIDIRECTIONAL
this.paper = paper;
} else {
log().warn("Tried to attach myself to a null Paper");
}
return this;
}
[...]
}
Note the bidirectional links and that there are several overloaded versions of
the setDatasets(...) method for different collection argument types.
The problem is that using a Castor XML mapping like this:
<class name="cedar.hepdata.model.Paper">
...
<field name="Datasets"
type="cedar.hepdata.model.Dataset"
collection="sortedset">
<bind-xml name="dataset" node="element"/>
</field>
</class>
doesn't seem to call any of the methods defined above: I don't see any warning
log messages coming out from Paper when I run an unmarshaller. The constructed
Paper does have a correct set of Datasets which I can read out, so something is
working, but none of those Datasets has a valid reference to its parent Paper -
the thing that would work if Paper.addDataset(...) was being called.
[The mapping documentation page
http://www.castor.org/xml-mapping.html#3.4-The-%3Cfield%3E-element suggests
that if I supply a set-method explicitly then an add... method will be used if
found, which is what I really want to happen: addDataset() has some specific
functionality that I want Castor to make use of. But if I adapt the <field> tag
in the mapping to include 'set-method="setDatasets"', then the unmarshalling
seems to ignore the Dataset collection entirely!]
Am I just missing something obvious here? My questions, in short, are
* How is Castor currently creating the collection, since none of my methods
seem to be being called?
* How can I make Castor add Dataset elements to a Paper during unmarshalling
using my Paper.addDataset() method instead of whatever it's currently doing?
Thanks for any help you can offer, and sorry about the length of this email!
Andy
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email