使用JAXB将xml映射到jpa实体

编程入门 行业动态 更新时间:2024-10-27 03:28:37
本文介绍了使用JAXB将xml映射到jpa实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以使用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

  • blog.bdoughan/2010/07/jpa-entities-to-xml-bidirectional.html
  • http://bdoughan.blogspot / 2010/08 / creating-restful-web-service-part-25.html
  • blog.bdoughan/2010/07/jpa-entities-to-xml-bidirectional.html
  • bdoughan.blogspot/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

  • wiki.eclipse/EclipseLink/Examples/MOXy/JPA/CompoundPrimaryKeys
  • wiki.eclipse/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/EclipseLink/Examples/MOXy/JPA/EmbeddedIdClass
  • wiki.eclipse/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.

  • blog.bdoughan/2011/06/moxy-extensible-models-multi-tenant.html
  • wiki.eclipse/ EclipseLink / Examples / JPA / Extensibility
  • blog.bdoughan/2011/06/moxy-extensible-models-multi-tenant.html
  • wiki.eclipse/EclipseLink/Examples/JPA/Extensibility

更多推荐

使用JAXB将xml映射到jpa实体

本文发布于:2023-08-01 09:12:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   JAXB   xml   jpa

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!