Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/MultipleBindingsFiles"

(New page: == Introduction == EclipseLink MOXy provides the ability to augment the annotation metadata with an XML bindings file. With this feature, EclipseLink can now process multiple bindings fil...)
 
Line 6: Line 6:
 
With the metadata layered in this way a JAXBContext could be created to represent any version of the XML document.  
 
With the metadata layered in this way a JAXBContext could be created to represent any version of the XML document.  
  
== Java Model ==
 
 
By using this in conjunction with a multi-tenant architecture, you can define a base set of metadata using one XML Bindings file, and then an additional file for each tenant. This example will be based on the object model from the [http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Extensible EclipseLink Moxy Extensibile] example.
 
By using this in conjunction with a multi-tenant architecture, you can define a base set of metadata using one XML Bindings file, and then an additional file for each tenant. This example will be based on the object model from the [http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Extensible EclipseLink Moxy Extensibile] example.
 +
 +
== Java Model ==
 +
'''ExtensibleBase'''
 +
<source lang="java">
 +
package examples.virtual;
 +
 +
import java.util.HashMap;
 +
import java.util.Map;
 +
 +
import javax.xml.bind.annotation.XmlTransient;
 +
 +
import org.eclipse.persistence.oxm.annotations.XmlVirtualAccessMethods;
 +
 +
@XmlTransient
 +
@XmlVirtualAccessMethods(setMethod="put")
 +
public class ExtensibleBase {
 +
 +
    private Map<String, Object> extensions = new HashMap<String, Object>();
 +
 +
    public <T> T get(String property) {
 +
        return (T) extensions.get(property);
 +
    }
 +
 +
    public void put(String property, Object value) {
 +
        extensions.put(property, value);
 +
    }
 +
}
 +
</source>
 +
 +
'''Customer'''
 +
 +
The Customer class will be extensible since it inherits from a domain class that has been annotated with '''@XmlVirtualAccessMethods'''.
 +
 +
<source lang="java">
 +
package examples.virtual;
 +
 +
import javax.xml.bind.annotation.XmlRootElement;
 +
 +
public class Customer extends ExtensibleBase {
 +
 +
    private String firstName;
 +
    private String lastName;
 +
    private Address billingAddress;
 +
 +
    public String getFirstName() {
 +
        return firstName;
 +
    }
 +
 +
    public void setFirstName(String firstName) {
 +
        this.firstName = firstName;
 +
    }
 +
 +
    public String getLastName() {
 +
        return lastName;
 +
    }
 +
 +
    public void setLastName(String lastName) {
 +
        this.lastName = lastName;
 +
    }
 +
 +
    public Address getBillingAddress() {
 +
        return billingAddress;
 +
    }
 +
 +
    public void setBillingAddress(Address billingAddress) {
 +
        this.billingAddress = billingAddress;
 +
    }
 +
 +
}
 +
</source>
 +
 +
'''Address'''
 +
 +
It is not necessary to have every class in your model be extensible.  In this example the Address class will not have any virtual properties.
 +
 +
<source lang="java">
 +
package examples.virtual;
 +
 +
public class Address {
 +
 +
    private String street;
 +
 +
    public String getStreet() {
 +
        return street;
 +
    }
 +
 +
    public void setStreet(String street) {
 +
        this.street = street;
 +
    }
 +
 +
}
 +
</source>
 +
 +
'''PhoneNumber'''
 +
 +
Like Customer, PhoneNumber will be an extensible class.
 +
 +
<source lang="java">
 +
package examples.virtual;
 +
 +
import javax.xml.bind.annotation.XmlValue;
 +
 +
public class PhoneNumber extends ExtensibleBase {
 +
 +
    private String number;
 +
 +
    public String getNumber() {
 +
        return number;
 +
    }
 +
 +
    public void setNumber(String number) {
 +
        this.number = number;
 +
    }
 +
 +
}
 +
</source>

Revision as of 12:48, 15 June 2011

Introduction

EclipseLink MOXy provides the ability to augment the annotation metadata with an XML bindings file. With this feature, EclipseLink can now process multiple bindings files from different locations, and create a merged set of metadata.

Sample use case: 1. Initial metadata is specified with a metadata file. 2. Second version modifies the metadata with an additional XML bindings file. 3. Subsequent versions continue to modify with additional bindings files.

With the metadata layered in this way a JAXBContext could be created to represent any version of the XML document.

By using this in conjunction with a multi-tenant architecture, you can define a base set of metadata using one XML Bindings file, and then an additional file for each tenant. This example will be based on the object model from the EclipseLink Moxy Extensibile example.

Java Model

ExtensibleBase

package examples.virtual;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.xml.bind.annotation.XmlTransient;
 
import org.eclipse.persistence.oxm.annotations.XmlVirtualAccessMethods;
 
@XmlTransient
@XmlVirtualAccessMethods(setMethod="put")
public class ExtensibleBase {
 
    private Map<String, Object> extensions = new HashMap<String, Object>();
 
    public <T> T get(String property) {
        return (T) extensions.get(property);
    }
 
    public void put(String property, Object value) {
        extensions.put(property, value);
    }
}

Customer

The Customer class will be extensible since it inherits from a domain class that has been annotated with @XmlVirtualAccessMethods.

package examples.virtual;
 
import javax.xml.bind.annotation.XmlRootElement;
 
public class Customer extends ExtensibleBase {
 
    private String firstName;
    private String lastName;
    private Address billingAddress;
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    public Address getBillingAddress() {
        return billingAddress;
    }
 
    public void setBillingAddress(Address billingAddress) {
        this.billingAddress = billingAddress;
    }
 
}

Address

It is not necessary to have every class in your model be extensible. In this example the Address class will not have any virtual properties.

package examples.virtual;
 
public class Address {
 
    private String street;
 
    public String getStreet() {
        return street;
    }
 
    public void setStreet(String street) {
        this.street = street;
    }
 
}

PhoneNumber

Like Customer, PhoneNumber will be an extensible class.

package examples.virtual;
 
import javax.xml.bind.annotation.XmlValue;
 
public class PhoneNumber extends ExtensibleBase {
 
    private String number;
 
    public String getNumber() {
        return number;
    }
 
    public void setNumber(String number) {
        this.number = number;
    }
 
}