Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] XMLChoiceCollectionMappingn that maps the same xpath to different classes?

Hi Magnus,

You're correct that this behavior isn't supported by the XMLChoiceCollectionMapping. This looks like (from your classes) that this is more of an inheritence use case. With inheritence, a class indicator field is used (typically an xsi:type) to indicate which subclass to instantiate during unmarshal.

This is configured using the InheritancePolicy on the descriptors for these types, and the class indicator values should reference the complex types in your schema. For example (assuming BuildingActivityReference is the root of your hierarchy, note, Java inheritance doesn't have to exist for this to work):

buildingActivityReferenceDescriptor.getInheritancePolicy().setClassIndicatorField("@xml:type");
buildingActivityReferenceDescriptor.getInheritancePolicy().addClassIndicator(BuildingActivityReference.class, "building-activity-reference-type"); buildingActivityReferenceDescriptor.getInheritencePolicy().addClassIndicator(TaxationUnitActivityReference.class, "taxation-unit-activity-reference-type");
buildingActivityReferenceDescriptor.getInheritencePolicy().setShouldReadSubclasses(true);
...
taxationUnitActivityReferenceDescriptor.getInheritancePolicy().setParentClass(BuildingActivityReference.class);

Then you would just use an XMLCompositeCollectionMapping:

XMLCompositeCollectionMapping mapping = new XMLCompositeCollectionMapping();
mapping.setXPath("ex:exchangeObjectReference");
mapping.setReferenceClass(BuildingActivityReference.class);

Then depending on the value of the xsi:type attribute it would read in the appropriate subclass. Your XML would look something like:

<ex:exchangeObjectReference xsi:type="taxation-unit-activity-reference-type">
 ...
</ex:exchangeObjectReference>
<ex:exchangeObjectReference xsi:type="building-unit-activity-reference-type">
...
</ex:exchangeObjectReference>

I hope that helps, if not, let me know.

-Matt

Magnus Heino wrote:

Hi.

Is it possible to create a mapping using XMLChoiceCollectionMappingn
that maps the same xpath to different classes?

Something like:

XMLChoiceCollectionMapping references = new XMLChoiceCollectionMapping();
references.addChoiceElement("ex:exchangeObjectReference",
BuildingActivityReference.class);
references.addChoiceElement("ex:exchangeObjectReference",
TaxationUnitActivityReference.class);

where it would first try to map it as a BuildingActivityReference, if
that doesn't work, try a TaxationUnitActivityReference.

Since the addChoiceElement call stores things in a Map, I guess this
isn't supported. Or should I use some other type of mapping? Or does
anyone have any suggestions of how I could implement this myself?

Thanks!

/Magnus Heino
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top