You can define multiple mappings within a single binding, as already demonstrated in the last collections examples. These mappings may all be top-level (children of the binding element), or may be nested within other mapping definitions. Nested mapping definitions are only usable within the context of the containing mapping. In general, it's not a good idea to nest mapping definitions more than one level deep, or inside mappings other than the one used for the root element of your documents, because of performance concerns.
Figure 12 is a trivial example of using multiple
mappings. The first mapping in this example binds the root customer
element to the
Using empty structure elements to reference mappings, as shown in Figure 12, is most useful when an object reference can be to instances of different classes. When there's no mapping defined for the exact type of the object reference associated with the structure, but there are one or more mappings for assignment-compatible types (such as subclasses), JiBX will accept any of the assignment-compatible types at runtime and use the appropriate mapping.
Figure 13 demonstrates using an empty
structure element with no mapping matching the object type. The
only change from the last example is that I changed
the type of the
You can force an empty structure element to use a particular mapping with the map-as attribute. This restricts the value for the referenced object to always be of the type of that mapping. It's generally a good idea to use map-as when you're expecting to use a specific mapping, even when JiBX will automatically select that mapping (as in Figure 12), just to make the linkage to a particular mapping explicit and avoid any potential confusion. I'll show some examples of using the map-as attribute later on this page.
All the mapping examples I've used so far are normal mappings, each relating an element name to instances of a particular class. JiBX also lets you define abstract mappings, which are essentially anonymous bindings for classes. Abstract mappings can be referenced from different contexts with different element names, or with no name at all. Figure 14 shows an example using an abstract mapping.Figure 14. Simple abstract mapping
This binding defines normal mappings for two classes, the
You can define multiple abstract mappings for the same class, using names to
distinguish between the mappings. Figure 15 shows a
modified version of the last example, where I've defined two different abstract
mappings for the
The Figure 15 changes have no effect on the document
with the customer root element, since the abstract mapping used for the
You can use interfaces as well as regular classes for abstract mappings, which is useful when the interface defines get/set methods to be used by the mapping. You can also use interfaces and abstract classes with a normal mapping definition in some circumstances - the basic requirement here is that there has to be a way to create an instance of the interface or abstract class when unmarshalling (as with a factory method, discussed in User extension method hooks).
mapping definitions are normally used for your own data classes, and those classes are directly modified by the JiBX binding compiler. You can also define mappings for unmodifiable classes, such as system classes or ones in jar files (though you can make the classes from a jar file modifiable by just unpacking the jar before running the binding compiler, then repacking it after). When a mapping is defined for an unmodifiable class, the mapping can only be used within the context of some other mapping.
Besides the "free standing" mappings you've seen so far, you can also define extension mappings which are linked to other mappings. Each extension mapping references some base mapping. By attaching itself to that base mapping, the extension mapping becomes an alternative to the base mapping anywhere the base mapping is invoked in the binding. When marshalling, the actual type of the object instance determines which mapping is applied, and hence the element name (and actual representation) used in the generated XML. When unmarshalling, the element name is used to select the extension mapping to be applied and hence the type of object to be used for the unmarshalled data.
Extension is easiest in the case where the base mapping class is never used directly. In this case you can define an abstract mapping as the root for extension, then add normal mappings which extend that abstract mapping for each substitute you want to allow in your binding. Figure 16 shows a simple example of this approach, with a base class containing some common information and a pair of subclasses providing additional details. This sort of polymorphic class hierarchy is a common use case for extension, but not the only use case.Figure 16. Abstract and extension mappings
In Figure 16 I've changed my earlier example code to
support two types of customers, persons and companies. The
I've highlighted the handling of the base
The base for an extension mapping doesn't have to be an abstract mapping. It's generally easiest to structure your extensions to use an abstract base when you can, but there are cases where that's just not possible. For example, if instances of your base mapping class can be used directly (rather than only instances of the extension mapping classes), you need to define a concrete mapping for that root class. Figure 17 gives an example of this situation, again using a set of related classes.Figure 17. Extending a concrete mapping
The difference from the last example is that in
Figure 17 the base
Even though the original intention of extension mappings was to represent polymorphism, they can also be used in other circumstances - there's no requirement that the classes handled by the extension mappings have any particular inheritance or implements relationship to the class handled by the base mapping. This flexibility can be useful when working with XML representations which assume a particular inheritance structure (in the form of substitution groups) that doesn't match the intent of your application code.
This page covers most of the mapping element options and usage, but see the <mapping> element details page for full details.