aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package javax.xml.bind.annotation; aoqi@0: aoqi@0: import java.lang.annotation.Target; aoqi@0: import java.lang.annotation.Retention; aoqi@0: import static java.lang.annotation.ElementType.*; aoqi@0: import static java.lang.annotation.RetentionPolicy.*; aoqi@0: aoqi@0: /** aoqi@0: *

aoqi@0: * Maps a JavaBean property to XML IDREF. aoqi@0: * aoqi@0: *

aoqi@0: * To preserve referential integrity of an object graph across XML aoqi@0: * serialization followed by a XML deserialization, requires an object aoqi@0: * reference to be marshalled by reference or containment aoqi@0: * appropriately. Annotations @XmlID and @XmlIDREF aoqi@0: * together allow a customized mapping of a JavaBean property's aoqi@0: * type by containment or reference. aoqi@0: * aoqi@0: *

Usage

aoqi@0: * The @XmlIDREF annotation can be used with the following aoqi@0: * program elements: aoqi@0: * aoqi@0: * aoqi@0: *

See "Package Specification" in javax.xml.bind.package javadoc for aoqi@0: * additional common information.

aoqi@0: * aoqi@0: *

The usage is subject to the following constraints: aoqi@0: *

aoqi@0: *

Example: Map a JavaBean property to xs:IDREF aoqi@0: * (i.e. by reference rather than by containment)

aoqi@0: *
aoqi@0:  *
aoqi@0:  *   //EXAMPLE: Code fragment
aoqi@0:  *   public class Shipping {
aoqi@0:  *       @XmlIDREF public Customer getCustomer();
aoqi@0:  *       public void setCustomer(Customer customer);
aoqi@0:  *       ....
aoqi@0:  *    }
aoqi@0:  *
aoqi@0:  *   <!-- Example: XML Schema fragment -->
aoqi@0:  *   <xs:complexType name="Shipping">
aoqi@0:  *     <xs:complexContent>
aoqi@0:  *       <xs:sequence>
aoqi@0:  *         <xs:element name="customer" type="xs:IDREF"/>
aoqi@0:  *         ....
aoqi@0:  *       </xs:sequence>
aoqi@0:  *     </xs:complexContent>
aoqi@0:  *   </xs:complexType>
aoqi@0:  *
aoqi@0:  * 
aoqi@0: * aoqi@0: * aoqi@0: *

Example 2: The following is a complete example of aoqi@0: * containment versus reference. aoqi@0: * aoqi@0: *

aoqi@0:  *    // By default, Customer maps to complex type xs:Customer
aoqi@0:  *    public class Customer {
aoqi@0:  *
aoqi@0:  *        // map JavaBean property type to xs:ID
aoqi@0:  *        @XmlID public String getCustomerID();
aoqi@0:  *        public void setCustomerID(String id);
aoqi@0:  *
aoqi@0:  *        // .... other properties not shown
aoqi@0:  *    }
aoqi@0:  *
aoqi@0:  *
aoqi@0:  *   // By default, Invoice maps to a complex type xs:Invoice
aoqi@0:  *   public class Invoice {
aoqi@0:  *
aoqi@0:  *       // map by reference
aoqi@0:  *       @XmlIDREF public Customer getCustomer();
aoqi@0:  *       public void setCustomer(Customer customer);
aoqi@0:  *
aoqi@0:  *      // .... other properties not shown here
aoqi@0:  *   }
aoqi@0:  *
aoqi@0:  *   // By default, Shipping maps to complex type xs:Shipping
aoqi@0:  *   public class Shipping {
aoqi@0:  *
aoqi@0:  *       // map by reference
aoqi@0:  *       @XmlIDREF public Customer getCustomer();
aoqi@0:  *       public void setCustomer(Customer customer);
aoqi@0:  *   }
aoqi@0:  *
aoqi@0:  *   // at least one class must reference Customer by containment;
aoqi@0:  *   // Customer instances won't be marshalled.
aoqi@0:  *   @XmlElement(name="CustomerData")
aoqi@0:  *   public class CustomerData {
aoqi@0:  *       // map reference to Customer by containment by default.
aoqi@0:  *       public Customer getCustomer();
aoqi@0:  *
aoqi@0:  *       // maps reference to Shipping by containment by default.
aoqi@0:  *       public Shipping getShipping();
aoqi@0:  *
aoqi@0:  *       // maps reference to Invoice by containment by default.
aoqi@0:  *       public Invoice getInvoice();
aoqi@0:  *   }
aoqi@0:  *
aoqi@0:  *   <!-- XML Schema mapping for above code frament -->
aoqi@0:  *
aoqi@0:  *   <xs:complexType name="Invoice">
aoqi@0:  *     <xs:complexContent>
aoqi@0:  *       <xs:sequence>
aoqi@0:  *         <xs:element name="customer" type="xs:IDREF"/>
aoqi@0:  *         ....
aoqi@0:  *       </xs:sequence>
aoqi@0:  *     </xs:complexContent>
aoqi@0:  *   </xs:complexType>
aoqi@0:  *
aoqi@0:  *   <xs:complexType name="Shipping">
aoqi@0:  *     <xs:complexContent>
aoqi@0:  *       <xs:sequence>
aoqi@0:  *         <xs:element name="customer" type="xs:IDREF"/>
aoqi@0:  *         ....
aoqi@0:  *       </xs:sequence>
aoqi@0:  *     </xs:complexContent>
aoqi@0:  *   </xs:complexType>
aoqi@0:  *
aoqi@0:  *   <xs:complexType name="Customer">
aoqi@0:  *     <xs:complexContent>
aoqi@0:  *       <xs:sequence>
aoqi@0:  *         ....
aoqi@0:  *       </xs:sequence>
aoqi@0:  *       <xs:attribute name="CustomerID" type="xs:ID"/>
aoqi@0:  *     </xs:complexContent>
aoqi@0:  *   </xs:complexType>
aoqi@0:  *
aoqi@0:  *   <xs:complexType name="CustomerData">
aoqi@0:  *     <xs:complexContent>
aoqi@0:  *       <xs:sequence>
aoqi@0:  *         <xs:element name="customer" type="xs:Customer"/>
aoqi@0:  *         <xs:element name="shipping" type="xs:Shipping"/>
aoqi@0:  *         <xs:element name="invoice"  type="xs:Invoice"/>
aoqi@0:  *       </xs:sequence>
aoqi@0:  *     </xs:complexContent>
aoqi@0:  *   </xs:complexType>
aoqi@0:  *
aoqi@0:  *   <xs:element name"customerData" type="xs:CustomerData"/>
aoqi@0:  *
aoqi@0:  *   <!-- Instance document conforming to the above XML Schema -->
aoqi@0:  *    <customerData>
aoqi@0:  *       <customer customerID="Alice">
aoqi@0:  *           ....
aoqi@0:  *       </customer>
aoqi@0:  *
aoqi@0:  *       <shipping customer="Alice">
aoqi@0:  *           ....
aoqi@0:  *       </shipping>
aoqi@0:  *
aoqi@0:  *       <invoice customer="Alice">
aoqi@0:  *           ....
aoqi@0:  *       </invoice>
aoqi@0:  *   </customerData>
aoqi@0:  *
aoqi@0:  * 
aoqi@0: * aoqi@0: *

Example 3: Mapping List to repeating element of type IDREF aoqi@0: *

aoqi@0:  *     // Code fragment
aoqi@0:  *     public class Shipping {
aoqi@0:  *         @XmlIDREF
aoqi@0:  *         @XmlElement(name="Alice")
aoqi@0:  *             public List customers;
aoqi@0:  *     }
aoqi@0:  *
aoqi@0:  *     <!-- XML schema fragment -->
aoqi@0:  *     <xs:complexType name="Shipping">
aoqi@0:  *       <xs:sequence>
aoqi@0:  *         <xs:choice minOccurs="0" maxOccurs="unbounded">
aoqi@0:  *           <xs:element name="Alice" type="xs:IDREF"/>
aoqi@0:  *         </xs:choice>
aoqi@0:  *       </xs:sequence>
aoqi@0:  *     </xs:complexType>
aoqi@0:  * 
aoqi@0: * aoqi@0: *

Example 4: Mapping a List to a list of elements of type IDREF. aoqi@0: *

aoqi@0:  *     //Code fragment
aoqi@0:  *     public class Shipping {
aoqi@0:  *         @XmlIDREF
aoqi@0:  *         @XmlElements(
aoqi@0:  *             @XmlElement(name="Alice", type="Customer.class")
aoqi@0:  *              @XmlElement(name="John", type="InternationalCustomer.class")
aoqi@0:  *         public List customers;
aoqi@0:  *     }
aoqi@0:  *
aoqi@0:  *     <!-- XML Schema fragment -->
aoqi@0:  *     <xs:complexType name="Shipping">
aoqi@0:  *       <xs:sequence>
aoqi@0:  *         <xs:choice minOccurs="0" maxOccurs="unbounded">
aoqi@0:  *           <xs:element name="Alice" type="xs:IDREF"/>
aoqi@0:  *           <xs:element name="John" type="xs:IDREF"/>
aoqi@0:  *         </xs:choice>
aoqi@0:  *       </xs:sequence>
aoqi@0:  *     </xs:complexType>
aoqi@0:  * 
aoqi@0: * @author Sekhar Vajjhala, Sun Microsystems, Inc. aoqi@0: * @see XmlID aoqi@0: * @since JAXB2.0 aoqi@0: */ aoqi@0: aoqi@0: @Retention(RUNTIME) @Target({FIELD, METHOD}) aoqi@0: public @interface XmlIDREF {}