使用JAXB将xml映射到jpa实体 [英] Mapping xml to jpa entities using JAXB
问题描述
是否可以使用JAXB将xml映射到jpa实体? Eclipselink Moxy会有帮助吗?
Isn't it possible to map xml to jpa entities using JAXB? Will Eclipselink Moxy be helpful?
推荐答案
注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB 2(JSR-222) 专家组。
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
是的,您可以将JPA实体映射到XML,以下是 EclipseLink JAXB(MOXy)使这更容易的一些方法。
Yes you can map JPA entities to XML, and the following are some ways that EclipseLink JAXB (MOXy) makes this easier.
<强> 1。双向映射
客户
Customer
import javax.persistence.*;
@Entity
public class Customer {
@Id
private long id;
@OneToOne(mappedBy="customer", cascade={CascadeType.ALL})
private Address address;
}
地址
Address
import javax.persistence.*;
import org.eclipse.persistence.oxm.annotations.*;
@Entity
public class Address implements Serializable {
@Id
private long id;
@OneToOne
@JoinColumn(name="ID")
@MapsId
@XmlInverseReference(mappedBy="address")
private Customer customer;
}
更多信息
For More Information
- http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
- http://bdoughan.blogspot .com / 2010/08 / creating-restful-web-service-part-25.html
- http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
- http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-25.html
2。映射复合关键关系
我们通常会考虑将对象树映射到XML,但是JAXB支持使用 @XmlID
/ @XmlIDREF
映射表示图形的节点之间的关系。标准机制是一个键,一个外键。 JPA支持复合键的概念,MOXy也使用 @XmlKey
和 @XmlJoinNodes
(类似于 JPA中的@XmlJoinColumns
。
We normally think of mapping a tree of objects to XML, however JAXB supports using the combination of @XmlID
/@XmlIDREF
to map relationship between nodes representing a graph. The standard mechanism is one key, to one foreign key. JPA supports the concept of composite keys and so does MOXy using @XmlKey
and @XmlJoinNodes
(similar to @XmlJoinColumns
in JPA).
员工
Employee
@Entity
@IdClass(EmployeeId.class)
public class Employee {
@Id
@Column(name="E_ID")
@XmlID
private BigDecimal eId;
@Id
@XmlKey
private String country;
@OneToMany(mappedBy="contact")
@XmlInverseReference(mappedBy="contact")
private List<PhoneNumber> contactNumber;
}
PhoneNumber
PhoneNumber
@Entity
public class PhoneNumber {
@ManyToOne
@JoinColumns({
@JoinColumn(name="E_ID", referencedColumnName = "E_ID"),
@JoinColumn(name="E_COUNTRY", referencedColumnName = "COUNTRY")
})
@XmlJoinNodes( {
@XmlJoinNode(xmlPath="contact/id/text()", referencedXmlPath="id/text()"),
@XmlJoinNode(xmlPath="contact/country/text()", referencedXmlPath="country/text()")
})
private Employee contact;
}
更多信息
For More Information
- http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA/CompoundPrimaryKeys
3。 MOXy允许复合键和嵌入键
JPA也可以使用嵌入的键类来表示复合键。 MOXy还支持这种复合键。
JPA can also use embedded key classes to represent composite keys. MOXy also supports this style of composite keys.
更多信息
For More Information
- http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA/EmbeddedIdClass
4。 EclipseLink JAXB(MOXy)和EclipseLink JPA具有共享概念
EclipseLink提供共享共同核心的JAXB和JPA实现。这意味着它们共享许多相同的概念,例如:
EclipseLink provides both JAXB and JPA implementations that share a common core. This means that they share many of the same concepts, such as:
虚拟访问方法
Virtual Access Methods
EclipseLink支持虚拟属性的概念。在创建需要按租户自定义的多租户应用程序时,这非常有用。 EclipseLink的JPA和JAXB实现都支持这个概念。
EclipseLink supports the concept of virtual properties. This is useful when creating a multi-tenant application where you want per-tenant customizations. This concept is upported in both EclipseLink's JPA and JAXB implementations.
- http://blog.bdoughan.com/2011/06/moxy-extensible-models-multi-tenant.html
- http://wiki.eclipse.org/ EclipseLink / Examples / JPA / Extensibility
- http://blog.bdoughan.com/2011/06/moxy-extensible-models-multi-tenant.html
- http://wiki.eclipse.org/EclipseLink/Examples/JPA/Extensibility
这篇关于使用JAXB将xml映射到jpa实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!