197 lines
8.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="header">
<div class="subTitle">
javax.xml.bind.annotation
</div>
<h2 class="title" title="Annotation Type XmlIDREF">Annotation Type XmlIDREF</h2>
</div><div class="contentContainer">
<div class="description">
<ul class="blockList">
<li class="blockList">
<hr/> <br/> <pre><a href="../../../../java/lang/annotation/Retention.html" title="annotation in java.lang.annotation">@Retention</a>(<a href="../../../../java/lang/annotation/Retention.html#value--">value</a>=<a href="../../../../java/lang/annotation/RetentionPolicy.html#RUNTIME">RUNTIME</a>)
<a href="../../../../java/lang/annotation/Target.html" title="annotation in java.lang.annotation">@Target</a>(<a href="../../../../java/lang/annotation/Target.html#value--">value</a>={<a href="../../../../java/lang/annotation/ElementType.html#FIELD">字段</a>,<a href="../../../../java/lang/annotation/ElementType.html#METHOD">METHOD</a>})
public @interface <span class="memberNameLabel">XmlIDREF</span></pre>
<div class="block">
<p> <span>将JavaBean属性映射到XML IDREF。</span> </p>
<p> <span>为了保持XML序列化之后的XML反序列化对象图的引用完整性需要通过引用或遏制来对对象引用进行编组。</span> <span>注释<tt>@XmlID</tt><tt>@XmlIDREF</tt>一起允许通过遏制或引用定制映射JavaBean属性的类型。</span> </p>
<p> <span><b>用法</b></span> </p>
<span><tt>@XmlIDREF</tt>注释可以与以下程序元素一起使用:</span>
<ul>
<li> <span>一个JavaBean属性</span> </li>
<li> <span>非静态,非瞬态场</span> </li>
</ul>
<p> <span>有关其他常见信息请参阅javax.xml.bind.package javadoc中的“Package Specification”。</span> </p>
<p> <span>用法受以下限制:</span> </p>
<ul>
<li> <span>如果字段或属性的类型是集合类型,则集合项类型必须包含一个注释为<tt>@XmlID</tt>的属性或字段。</span> </li>
<li> <span>如果字段或属性是单值,则属性或字段的类型必须包含用<tt>@XmlID注释</tt>的属性或字段。</span> <p> <span>注意如果集合项目类型或属性类型对于非集合类型为java.lang.Object则该实例必须包含带有<tt>@XmlID</tt>属性注释的属性/字段。</span> </p></li>
<li> <span>此注释可与以下注释一起使用: <a href="../../../../javax/xml/bind/annotation/XmlElement.html" title="javax.xml.bind.annotation中的注释"><code>XmlElement</code></a> <a href="../../../../javax/xml/bind/annotation/XmlAttribute.html" title="javax.xml.bind.annotation中的注释"><code>XmlAttribute</code></a> <a href="../../../../javax/xml/bind/annotation/XmlList.html" title="javax.xml.bind.annotation中的注释"><code>XmlList</code></a><a href="../../../../javax/xml/bind/annotation/XmlElements.html" title="javax.xml.bind.annotation中的注释"><code>XmlElements</code></a></span> </li>
</ul>
<p> <span><b>示例:</b>将JavaBean属性映射到<tt>xs:IDREF</tt> (即通过引用而不是通过遏制)</span> </p>
<pre> <span>//EXAMPLE: Code fragment
public class Shipping {
@XmlIDREF public Customer getCustomer();
public void setCustomer(Customer customer);
....
}
&lt;!-- Example: XML Schema fragment --&gt;
&lt;xs:complexType name="Shipping"&gt;
&lt;xs:complexContent&gt;
&lt;xs:sequence&gt;
&lt;xs:element name="customer" type="xs:IDREF"/&gt;
....
&lt;/xs:sequence&gt;
&lt;/xs:complexContent&gt;
&lt;/xs:complexType&gt;</span> </pre>
<p> <span><b>示例2</b>以下是遏制与参考的完整示例。</span> </p>
<pre> <span>// By default, Customer maps to complex type <tt>xs:Customer</tt>
public class Customer {
// map JavaBean property type to <tt>xs:ID</tt>
@XmlID public String getCustomerID();
public void setCustomerID(String id);
// .... other properties not shown
}
// By default, Invoice maps to a complex type <tt>xs:Invoice</tt>
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 <tt>xs:Shipping</tt>
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();
}
&lt;!-- XML Schema mapping for above code frament --&gt;
&lt;xs:complexType name="Invoice"&gt;
&lt;xs:complexContent&gt;
&lt;xs:sequence&gt;
&lt;xs:element name="customer" type="xs:IDREF"/&gt;
....
&lt;/xs:sequence&gt;
&lt;/xs:complexContent&gt;
&lt;/xs:complexType&gt;
&lt;xs:complexType name="Shipping"&gt;
&lt;xs:complexContent&gt;
&lt;xs:sequence&gt;
&lt;xs:element name="customer" type="xs:IDREF"/&gt;
....
&lt;/xs:sequence&gt;
&lt;/xs:complexContent&gt;
&lt;/xs:complexType&gt;
&lt;xs:complexType name="Customer"&gt;
&lt;xs:complexContent&gt;
&lt;xs:sequence&gt;
....
&lt;/xs:sequence&gt;
&lt;xs:attribute name="CustomerID" type="xs:ID"/&gt;
&lt;/xs:complexContent&gt;
&lt;/xs:complexType&gt;
&lt;xs:complexType name="CustomerData"&gt;
&lt;xs:complexContent&gt;
&lt;xs:sequence&gt;
&lt;xs:element name="customer" type="xs:Customer"/&gt;
&lt;xs:element name="shipping" type="xs:Shipping"/&gt;
&lt;xs:element name="invoice" type="xs:Invoice"/&gt;
&lt;/xs:sequence&gt;
&lt;/xs:complexContent&gt;
&lt;/xs:complexType&gt;
&lt;xs:element name"customerData" type="xs:CustomerData"/&gt;
&lt;!-- Instance document conforming to the above XML Schema --&gt;
&lt;customerData&gt;
&lt;customer customerID="Alice"&gt;
....
&lt;/customer&gt;
&lt;shipping customer="Alice"&gt;
....
&lt;/shipping&gt;
&lt;invoice customer="Alice"&gt;
....
&lt;/invoice&gt;
&lt;/customerData&gt;</span> </pre>
<p> <span><b>示例3</b>将列表映射到IDREF类型的重复元素</span> </p>
<pre> <span>// Code fragment
public class Shipping {
@XmlIDREF
@XmlElement(name="Alice")
public List customers;
}
&lt;!-- XML schema fragment --&gt;
&lt;xs:complexType name="Shipping"&gt;
&lt;xs:sequence&gt;
&lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt;
&lt;xs:element name="Alice" type="xs:IDREF"/&gt;
&lt;/xs:choice&gt;
&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;</span> </pre>
<p> <span><b>示例4</b>将列表映射到IDREF类型的元素列表。</span> </p>
<pre> <span>//Code fragment
public class Shipping {
@XmlIDREF
@XmlElements(
@XmlElement(name="Alice", type="Customer.class")
@XmlElement(name="John", type="InternationalCustomer.class")
public List customers;
}
&lt;!-- XML Schema fragment --&gt;
&lt;xs:complexType name="Shipping"&gt;
&lt;xs:sequence&gt;
&lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt;
&lt;xs:element name="Alice" type="xs:IDREF"/&gt;
&lt;xs:element name="John" type="xs:IDREF"/&gt;
&lt;/xs:choice&gt;
&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;</span> </pre>
</div>
<dl>
<dt>
<span class="simpleTagLabel">从以下版本开始:</span>
</dt>
<dd>
JAXB2.0
</dd>
<dt>
<span class="seeLabel">另请参见:</span>
</dt>
<dd>
<span><a href="../../../../javax/xml/bind/annotation/XmlID.html" title="javax.xml.bind.annotation中的注释"><code>XmlID</code></a></span>
</dd>
</dl> </li>
</ul>
</div>
</div>