The metadata for the virtual properties is supplied through MOXy's XML mapping file. Virtual properties are mapped in the same way as real properties. Some additional information is required including type (since this cannot be determined via reflection), and for collection properties a container type. The virtual properties defined below for Customer are "middleName", "shippingAddress" and "phoneNumbers". For PhoneNumber the virtual property is the "type" property.

The metadata for the virtual properties is supplied through MOXy's XML mapping file. Virtual properties are mapped in the same way as real properties. Some additional information is required including type (since this cannot be determined via reflection), and for collection properties a container type. The virtual properties defined below for Customer are "middleName", "shippingAddress" and "phoneNumbers". For PhoneNumber the virtual property is the "type" property.

Revision as of 09:47, 15 June 2011

Contents

Introduction

In a multi-tenant architecture, a single application runs on a server, serving multiple client organizations (tenants). Good multi-tenant applications allow per-tenant customizations. When these customizations are made to data, it can be difficult for the binding layer to handle them. JAXB is designed to work with domain models that have real fields and properties. EclipseLink MOXy 2.3 introduces the concept of virtual properties which can easily handle this use case. Virtual properties are defined by the MOXy metadata file, and provide a way to extend a class without modifying the source.

Java Model with @XmlVirtualAccessMethods annotation

The @XmlVirtualAccessMethods annotation is used to specify that a class is extensible. An extensible class is required to have a get method that returns a value by property name, and a set method that stores a value by property name. The default names for these methods are "get" and "set", and can be overridden with the @XmlVirtualAccessMethods annotation. Since we will have multiple extensible classes in this example, we'll configure a base class for this behaviour that extensible classes can extend. We will use the @XmlTransient annotation to prevent ExtensibleBase from being mapped as an inheritance relationship. The real properties represent the parts of the model that will be common to all tenants. The per-tenant extensions will be represented as virtual properties.

Tenant 1

The first tenant is an online sporting goods store, that requires the following extensions to their model:

Customer ID

Customer's middle name

Shipping address

A collection of contact phone numbers

Type of phone number (i.e. home, work, or cell)

The metadata for the virtual properties is supplied through MOXy's XML mapping file. Virtual properties are mapped in the same way as real properties. Some additional information is required including type (since this cannot be determined via reflection), and for collection properties a container type. The virtual properties defined below for Customer are "middleName", "shippingAddress" and "phoneNumbers". For PhoneNumber the virtual property is the "type" property.

The get/set methods are used on the domain model to interact with the real properties and the accessors defined on the @XmlVirtualAccessMethods annotation are used to interact with the virtual properties. The normal JAXB mechanisms are used for marshal and unmarshal operations: