Module  java.xml.bind

Annotation Type XmlIDREF



  • @Retention(RUNTIME)
    @Target({字段,METHOD})
    public @interface XmlIDREF

    将JavaBean属性映射到XML IDREF。

    为了保持XML序列化和XML反序列化之间的对象图的引用完整性,需要通过引用或遏制来适当地封送对象引用。 注释@XmlID@XmlIDREF一起允许通过遏制或引用定制映射JavaBean属性的类型。

    用法

    @XmlIDREF注释可以与以下程序元素一起使用:
    • 一个JavaBean属性
    • 非静态,非瞬态场

    有关其他常见信息,请参阅javax.xml.bind.package javadoc中的“Package Specification”。

    用法受以下限制:

    • 如果字段或属性的类型是集合类型,则集合项目类型必须包含用@XmlID注释的属性或字段。
    • 如果字段或属性是单值,则属性或字段的类型必须包含一个注释为@XmlID的属性或字段。

      注意:如果集合项目类型或属性类型(对于非集合类型)为java.lang.Object,则该实例必须包含带有@XmlID属性注释的属性/字段。

    • 此注释可与以下注释一起使用: XmlElementXmlAttributeXmlList ,并XmlElements

    示例:将JavaBean属性映射到xs:IDREF (即通过引用而不是通过遏制)

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

    示例2:以下是遏制与参考的完整示例。

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

    示例3:将列表映射到IDREF类型的重复元素

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

    示例4:将列表映射到IDREF类型的元素列表。

      //Code fragment
         public class Shipping {
             @XmlIDREF
             @XmlElements(
                 @XmlElement(name="Alice", type="Customer.class")
                  @XmlElement(name="John", type="InternationalCustomer.class")
             public List customers;
         }
      <!-- XML Schema fragment --> <xs:complexType name="Shipping"> <xs:sequence> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Alice" type="xs:IDREF"/> <xs:element name="John" type="xs:IDREF"/> </xs:choice> </xs:sequence> </xs:complexType>  
    从以下版本开始:
    1.6,JAXB 2.0
    另请参见:
    XmlID