1 /* 2 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.xml.bind.annotation; 27 28 import java.lang.annotation.Target; 29 import java.lang.annotation.Retention; 30 import static java.lang.annotation.ElementType.*; 31 import static java.lang.annotation.RetentionPolicy.*; 32 33 /** 34 * <p> 35 * Maps a JavaBean property to XML IDREF. 36 * 37 * <p> 38 * To preserve referential integrity of an object graph across XML 39 * serialization followed by a XML deserialization, requires an object 40 * reference to be marshalled by reference or containment 41 * appropriately. Annotations <tt>@XmlID</tt> and <tt>@XmlIDREF</tt> 42 * together allow a customized mapping of a JavaBean property's 43 * type by containment or reference. 44 * 45 * <p><b>Usage</b> </p> 46 * The <tt>@XmlIDREF</tt> annotation can be used with the following 47 * program elements: 48 * <ul> 49 * <li> a JavaBean property </li> 50 * <li> non static, non transient field </li> 51 * </ul> 52 * 53 * <p>See "Package Specification" in javax.xml.bind.package javadoc for 54 * additional common information.</p> 55 * 56 * <p> The usage is subject to the following constraints: 57 * <ul> 58 * 59 * <li> If the type of the field or property is a collection type, 60 * then the collection item type must contain a property or 61 * field annotated with <tt>@XmlID</tt>. </li> 62 * <li> If the field or property is single valued, then the type of 63 * the property or field must contain a property or field 64 * annotated with <tt>@XmlID</tt>. 65 * <p>Note: If the collection item type or the type of the 66 * property (for non collection type) is java.lang.Object, then 67 * the instance must contain a property/field annotated with 68 * <tt>@XmlID</tt> attribute. 69 * </li> 70 * <li> This annotation can be used with the following annotations: 71 * {@link XmlElement}, {@link XmlAttribute}, {@link XmlList}, 72 * and {@link XmlElements}.</li> 73 * 74 * </ul> 75 * <p><b>Example:</b> Map a JavaBean property to <tt>xs:IDREF</tt> 76 * (i.e. by reference rather than by containment)</p> 77 * <pre> 78 * 79 * //EXAMPLE: Code fragment 80 * public class Shipping { 81 * @XmlIDREF public Customer getCustomer(); 82 * public void setCustomer(Customer customer); 83 * .... 84 * } 85 * 86 * <!-- Example: XML Schema fragment --> 87 * <xs:complexType name="Shipping"> 88 * <xs:complexContent> 89 * <xs:sequence> 90 * <xs:element name="customer" type="xs:IDREF"/> 91 * .... 92 * </xs:sequence> 93 * </xs:complexContent> 94 * </xs:complexType> 95 * 96 * </pre> 97 * 98 * 99 * <p><b>Example 2: </b> The following is a complete example of 100 * containment versus reference. 101 * 102 * <pre> 103 * // By default, Customer maps to complex type <tt>xs:Customer</tt> 104 * public class Customer { 105 * 106 * // map JavaBean property type to <tt>xs:ID</tt> 107 * @XmlID public String getCustomerID(); 108 * public void setCustomerID(String id); 109 * 110 * // .... other properties not shown 111 * } 112 * 113 * 114 * // By default, Invoice maps to a complex type <tt>xs:Invoice</tt> 115 * public class Invoice { 116 * 117 * // map by reference 118 * @XmlIDREF public Customer getCustomer(); 119 * public void setCustomer(Customer customer); 120 * 121 * // .... other properties not shown here 122 * } 123 * 124 * // By default, Shipping maps to complex type <tt>xs:Shipping</tt> 125 * public class Shipping { 126 * 127 * // map by reference 128 * @XmlIDREF public Customer getCustomer(); 129 * public void setCustomer(Customer customer); 130 * } 131 * 132 * // at least one class must reference Customer by containment; 133 * // Customer instances won't be marshalled. 134 * @XmlElement(name="CustomerData") 135 * public class CustomerData { 136 * // map reference to Customer by containment by default. 137 * public Customer getCustomer(); 138 * 139 * // maps reference to Shipping by containment by default. 140 * public Shipping getShipping(); 141 * 142 * // maps reference to Invoice by containment by default. 143 * public Invoice getInvoice(); 144 * } 145 * 146 * <!-- XML Schema mapping for above code frament --> 147 * 148 * <xs:complexType name="Invoice"> 149 * <xs:complexContent> 150 * <xs:sequence> 151 * <xs:element name="customer" type="xs:IDREF"/> 152 * .... 153 * </xs:sequence> 154 * </xs:complexContent> 155 * </xs:complexType> 156 * 157 * <xs:complexType name="Shipping"> 158 * <xs:complexContent> 159 * <xs:sequence> 160 * <xs:element name="customer" type="xs:IDREF"/> 161 * .... 162 * </xs:sequence> 163 * </xs:complexContent> 164 * </xs:complexType> 165 * 166 * <xs:complexType name="Customer"> 167 * <xs:complexContent> 168 * <xs:sequence> 169 * .... 170 * </xs:sequence> 171 * <xs:attribute name="CustomerID" type="xs:ID"/> 172 * </xs:complexContent> 173 * </xs:complexType> 174 * 175 * <xs:complexType name="CustomerData"> 176 * <xs:complexContent> 177 * <xs:sequence> 178 * <xs:element name="customer" type="xs:Customer"/> 179 * <xs:element name="shipping" type="xs:Shipping"/> 180 * <xs:element name="invoice" type="xs:Invoice"/> 181 * </xs:sequence> 182 * </xs:complexContent> 183 * </xs:complexType> 184 * 185 * <xs:element name"customerData" type="xs:CustomerData"/> 186 * 187 * <!-- Instance document conforming to the above XML Schema --> 188 * <customerData> 189 * <customer customerID="Alice"> 190 * .... 191 * </customer> 192 * 193 * <shipping customer="Alice"> 194 * .... 195 * </shipping> 196 * 197 * <invoice customer="Alice"> 198 * .... 199 * </invoice> 200 * </customerData> 201 * 202 * </pre> 203 * 204 * <p><b>Example 3: </b> Mapping List to repeating element of type IDREF 205 * <pre> 206 * // Code fragment 207 * public class Shipping { 208 * @XmlIDREF 209 * @XmlElement(name="Alice") 210 * public List customers; 211 * } 212 * 213 * <!-- XML schema fragment --> 214 * <xs:complexType name="Shipping"> 215 * <xs:sequence> 216 * <xs:choice minOccurs="0" maxOccurs="unbounded"> 217 * <xs:element name="Alice" type="xs:IDREF"/> 218 * </xs:choice> 219 * </xs:sequence> 220 * </xs:complexType> 221 * </pre> 222 * 223 * <p><b>Example 4: </b> Mapping a List to a list of elements of type IDREF. 224 * <pre> 225 * //Code fragment 226 * public class Shipping { 227 * @XmlIDREF 228 * @XmlElements( 229 * @XmlElement(name="Alice", type="Customer.class") 230 * @XmlElement(name="John", type="InternationalCustomer.class") 231 * public List customers; 232 * } 233 * 234 * <!-- XML Schema fragment --> 235 * <xs:complexType name="Shipping"> 236 * <xs:sequence> 237 * <xs:choice minOccurs="0" maxOccurs="unbounded"> 238 * <xs:element name="Alice" type="xs:IDREF"/> 239 * <xs:element name="John" type="xs:IDREF"/> 240 * </xs:choice> 241 * </xs:sequence> 242 * </xs:complexType> 243 * </pre> 244 * @author Sekhar Vajjhala, Sun Microsystems, Inc. 245 * @see XmlID 246 * @since JAXB2.0 247 */ 248 249 @Retention(RUNTIME) @Target({FIELD, METHOD}) 250 public @interface XmlIDREF {}