|
|
(14 intermediate revisions by 2 users not shown) |
Line 1: |
Line 1: |
− | {{EclipseLink_UserGuide
| + | See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/shared_reference_relationships002.htm |
− | |info=y
| + | |
− | |api=y
| + | |
− | |apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlID.html javax.xml.bind.annotation.XmlID]
| + | |
− | * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlIDREF.html javax.xml.bind.annotation.XmlIDREF]
| + | |
− | }}
| + | |
− | | + | |
− | =Single Key=
| + | |
− | | + | |
− | To model non-privately-owned relationships, your "target" objects must have IDs (keys) defined, and your "source" object must use these IDs to map the relationship.
| + | |
− | | + | |
− | Relationships represented with keys use the '''@XmlID''' and '''@XmlIDREF''' annotations. Although the JAXB specification requires that the property marked with '''@XmlID''' be a '''String''', MOXy JAXB does not enforce this restriction.
| + | |
− | | + | |
− | In this example, each '''Employee''' has ''one'' '''manager''' but ''multiple'' '''reports'''.
| + | |
− | <source lang="java">
| + | |
− | @XmlAccessorType(XmlAccessType.FIELD)
| + | |
− | public class Employee {
| + | |
− | @XmlAttribute
| + | |
− | @XmlID
| + | |
− | private String id;
| + | |
− |
| + | |
− | @XmlAttribute
| + | |
− | private String name;
| + | |
− |
| + | |
− | @XmlIDREF
| + | |
− | private Employee manager;
| + | |
− |
| + | |
− | @XmlElement(name="report")
| + | |
− | @XmlIDREF
| + | |
− | private List<Employee> reports;
| + | |
− | | + | |
− | ...
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | Would produce the following XML:
| + | |
− | | + | |
− | <source lang="xml">
| + | |
− | <company>
| + | |
− | <employee id="1" name="Jane Doe">
| + | |
− | <report>2</report>
| + | |
− | <report>3</report>
| + | |
− | </employee>
| + | |
− | <employee id="2" name="John Smith">
| + | |
− | <manager>1</manager>
| + | |
− | </employee>
| + | |
− | <employee id="3" name="Anne Jones">
| + | |
− | <manager>1</manager>
| + | |
− | </employee>
| + | |
− | </company>
| + | |
− | </source>
| + | |
− | | + | |
− | The '''manager''' and '''reports''' elements contain the IDs of the '''Employee''' instances they are referencing.
| + | |
− | | + | |
− | Because the '''@XmlIDREF''' annotation is also compatible with the '''@XmlList''' annotation, the '''Employee''' object could be modeled as:
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | public class Employee {
| + | |
− | @XmlID
| + | |
− | @XmlAttribute
| + | |
− | private String id;
| + | |
− |
| + | |
− | @XmlAttribute
| + | |
− | private String name;
| + | |
− |
| + | |
− | @XmlIDREF
| + | |
− | private Employee manager;
| + | |
− |
| + | |
− | @XmlIDREF
| + | |
− | @XmlList
| + | |
− | private List<Employee> reports;
| + | |
− | | + | |
− | ...
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | Which would produce the following XML:
| + | |
− | <source lang="xml">
| + | |
− | <company>
| + | |
− | <employee id="1" name="Jane Doe">
| + | |
− | <reports>2 3</reports>
| + | |
− | </employee>
| + | |
− | <employee id="2" name="John Smith">
| + | |
− | <manager>1</manager>
| + | |
− | </employee>
| + | |
− | <employee id="3" name="Anne Jones">
| + | |
− | <manager>1</manager>
| + | |
− | </employee>
| + | |
− | </company>
| + | |
− | </source>
| + | |
− | | + | |
− | {{EclipseLink_MOXy
| + | |
− | |previous=[[EclipseLink/UserGuide/MOXy/Relationships/Shared Reference/Keys and Foreign Keys|Keys and Foreign Keys]]
| + | |
− | |up=[[EclipseLink/UserGuide/MOXy/Relationships/Shared Reference/Keys and Foreign Keys|Keys and Foreign Keys]]
| + | |
− | |next=[[EclipseLink/UserGuide/MOXy/Relationships/Shared Reference/Keys and Foreign Keys/Composite Key|Composite Key]]
| + | |
− | |version=2.2.0
| + | |
− | }}
| + | |