@Retention(value=RUNTIME) @Target(value={字段,METHOD}) public @interface XmlIDREF
将JavaBean属性映射到XML IDREF。
为了保持XML序列化之后的XML反序列化对象图的引用完整性,需要通过引用或遏制来对对象引用进行编组。 注释@XmlID和@XmlIDREF一起允许通过遏制或引用定制映射JavaBean属性的类型。
用法
@XmlIDREF注释可以与以下程序元素一起使用:有关其他常见信息,请参阅javax.xml.bind.package javadoc中的“Package Specification”。
用法受以下限制:
注意:如果集合项目类型或属性类型(对于非集合类型)为java.lang.Object,则该实例必须包含带有@XmlID属性注释的属性/字段。
XmlElement
, XmlAttribute
, XmlList
和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>
XmlID