1 /* 2 * Copyright (c) 2004, 2015, 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 marshaled 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 * {@code 86 * 87 * <!-- Example: XML Schema fragment --> 88 * <xs:complexType name="Shipping"> 89 * <xs:complexContent> 90 * <xs:sequence> 91 * <xs:element name="customer" type="xs:IDREF"/> 92 * .... 93 * </xs:sequence> 94 * </xs:complexContent> 95 * </xs:complexType> 96 * 97 * }</pre> 98 * 99 * 100 * <p><b>Example 2: </b> The following is a complete example of 101 * containment versus reference. 102 * 103 * <pre> 104 * // By default, Customer maps to complex type <tt>xs:Customer</tt> 105 * public class Customer { 106 * 107 * // map JavaBean property type to <tt>xs:ID</tt> 108 * @XmlID public String getCustomerID(); 109 * public void setCustomerID(String id); 110 * 111 * // .... other properties not shown 112 * } 113 * 114 * 115 * // By default, Invoice maps to a complex type <tt>xs:Invoice</tt> 116 * public class Invoice { 117 * 118 * // map by reference 119 * @XmlIDREF public Customer getCustomer(); 120 * public void setCustomer(Customer customer); 121 * 122 * // .... other properties not shown here 123 * } 124 * 125 * // By default, Shipping maps to complex type <tt>xs:Shipping</tt> 126 * public class Shipping { 127 * 128 * // map by reference 129 * @XmlIDREF public Customer getCustomer(); 130 * public void setCustomer(Customer customer); 131 * } 132 * 133 * // at least one class must reference Customer by containment; 134 * // Customer instances won't be marshalled. 135 * @XmlElement(name="CustomerData") 136 * public class CustomerData { 137 * // map reference to Customer by containment by default. 138 * public Customer getCustomer(); 139 * 140 * // maps reference to Shipping by containment by default. 141 * public Shipping getShipping(); 142 * 143 * // maps reference to Invoice by containment by default. 144 * public Invoice getInvoice(); 145 * } 146 * {@code 147 * 148 * <!-- XML Schema mapping for above code frament --> 149 * 150 * <xs:complexType name="Invoice"> 151 * <xs:complexContent> 152 * <xs:sequence> 153 * <xs:element name="customer" type="xs:IDREF"/> 154 * .... 155 * </xs:sequence> 156 * </xs:complexContent> 157 * </xs:complexType> 158 * 159 * <xs:complexType name="Shipping"> 160 * <xs:complexContent> 161 * <xs:sequence> 162 * <xs:element name="customer" type="xs:IDREF"/> 163 * .... 164 * </xs:sequence> 165 * </xs:complexContent> 166 * </xs:complexType> 167 * 168 * <xs:complexType name="Customer"> 169 * <xs:complexContent> 170 * <xs:sequence> 171 * .... 172 * </xs:sequence> 173 * <xs:attribute name="CustomerID" type="xs:ID"/> 174 * </xs:complexContent> 175 * </xs:complexType> 176 * 177 * <xs:complexType name="CustomerData"> 178 * <xs:complexContent> 179 * <xs:sequence> 180 * <xs:element name="customer" type="xs:Customer"/> 181 * <xs:element name="shipping" type="xs:Shipping"/> 182 * <xs:element name="invoice" type="xs:Invoice"/> 183 * </xs:sequence> 184 * </xs:complexContent> 185 * </xs:complexType> 186 * 187 * <xs:element name"customerData" type="xs:CustomerData"/> 188 * 189 * <!-- Instance document conforming to the above XML Schema --> 190 * <customerData> 191 * <customer customerID="Alice"> 192 * .... 193 * </customer> 194 * 195 * <shipping customer="Alice"> 196 * .... 197 * </shipping> 198 * 199 * <invoice customer="Alice"> 200 * .... 201 * </invoice> 202 * </customerData> 203 * 204 * }</pre> 205 * 206 * <p><b>Example 3: </b> Mapping List to repeating element of type IDREF 207 * <pre> 208 * // Code fragment 209 * public class Shipping { 210 * @XmlIDREF 211 * @XmlElement(name="Alice") 212 * public List customers; 213 * } 214 * {@code 215 * 216 * <!-- XML schema fragment --> 217 * <xs:complexType name="Shipping"> 218 * <xs:sequence> 219 * <xs:choice minOccurs="0" maxOccurs="unbounded"> 220 * <xs:element name="Alice" type="xs:IDREF"/> 221 * </xs:choice> 222 * </xs:sequence> 223 * </xs:complexType> 224 * }</pre> 225 * 226 * <p><b>Example 4: </b> Mapping a List to a list of elements of type IDREF. 227 * <pre> 228 * //Code fragment 229 * public class Shipping { 230 * @XmlIDREF 231 * @XmlElements( 232 * @XmlElement(name="Alice", type="Customer.class") 233 * @XmlElement(name="John", type="InternationalCustomer.class") 234 * public List customers; 235 * } 236 * {@code 237 * 238 * <!-- XML Schema fragment --> 239 * <xs:complexType name="Shipping"> 240 * <xs:sequence> 241 * <xs:choice minOccurs="0" maxOccurs="unbounded"> 242 * <xs:element name="Alice" type="xs:IDREF"/> 243 * <xs:element name="John" type="xs:IDREF"/> 244 * </xs:choice> 245 * </xs:sequence> 246 * </xs:complexType> 247 * }</pre> 248 * @author Sekhar Vajjhala, Sun Microsystems, Inc. 249 * @see XmlID 250 * @since 1.6, JAXB 2.0 251 */ 252 253 @Retention(RUNTIME) @Target({FIELD, METHOD}) 254 public @interface XmlIDREF {}