Werner Guttmann wrote:
> Andy, please see inline ...

Thanks for the help - it isn't working yet, though!

>> just want to know why my Castor mapping for an XML structure like this:
>>
>> <paper>
>>   <dataset datasetId="1">...</dataset>
>>   <dataset datasetId="2">...</dataset>
>>   ...
>> </paper>
>>
>> doesn't seem to call the Paper.setDatasets(...) or Paper.addDataset(...)
>> methods during unmarshalling? How *is* the paper's collection of datasets
>> getting populated?
>
> Basically, unless you provide an addMethod in the mapping file, Castor XMl
> - for collection types - will by default use the getter method to obtain a
> reference to the collection and call the add() (for e.g. list and set
> types) method on the collection itself.

Ah - I didn't realise that that was the meaning of the first sentence of the
"exceptions concerning collection fields" section here:
http://www.castor.org/xml-mapping.html#3.4-The-<field>-element . I guess that
when you say "provide an addMethod", you mean that I should define the
set-method attribute, and rely on the auto-detection of the add method as
described on that web page?

For definiteness, here's my mapping for the Datasets field in the Paper class:

<field name="Datasets" type="cedar.hepdata.model.Dataset"
collection="sortedset" set-method="setDatasets">
  <bind-xml name="dataset" node="element"/>
</field>

I suspected that the problem was that my set method was defined with the 
signature

public Paper setDatasets(SortedSet<Dataset> datasets)

i.e. a return type of Paper rather than void (it returns "this" so that add and
set methods can be chained). I tried changing this, though, and it made no
difference: is the return type *actually* checked? Anyway, here are the new set
and add methods defined in the Paper class:

public void setDatasets(SortedSet<Dataset> datasets) {
    System.out.println("Setting datasets");
    this.datasets.clear();
    for (Dataset d : datasets) addDataset(d);
}

public void addDataset(Dataset dataset) {
    System.out.println("Adding a dataset");
    ...
    dataset.setPaper(this);
    this.getDatasets().add(dataset);
}

(I also removed the setDatasets(Collection<Dataset>) and similar methods, to
make sure there's no source of confusion.)

As far as I can tell, these should definitely get called when the mapping above
is used. But when the set-method attribute is added, I don't see either of the
printouts from these methods during unmarshalling, and the datasets collection
ends up empty. If I try changing the value of set-method to something
non-existent an exception occurs, so it looks like Castor is finding the
methods but then not calling them. Help!!!

Thanks again,
Andy

-- 
Andy Buckley: CEDAR @ IPPP, Durham
Work: www.cedar.ac.uk
www.insectnation.org

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to