使用 JAXB 将 xml 映射到 jpa 实体

发布于 2024-12-19 11:56:28 字数 59 浏览 2 评论 0原文

是否可以使用 JAXB 将 xml 映射到 jpa 实体? Eclipselink Moxy 有帮助吗?

Isn't it possible to map xml to jpa entities using JAXB? Will Eclipselink Moxy be helpful?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-12-26 11:56:28

注意:我是EclipseLink JAXB (MOXy) 的领导者和 JAXB 2 的成员(JSR-222)专家组。

是的,您可以将 JPA 实体映射到 XML,下面是 EclipseLink JAXB (MOXy) 使这变得更容易的一些方法。

1.双向映射

客户

import javax.persistence.*;

@Entity
public class Customer {

    @Id
    private long id;

    @OneToOne(mappedBy="customer", cascade={CascadeType.ALL})
    private 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;

}

了解更多信息< /em>

2.映射复合键关系

我们通常会考虑将对象树映射到 XML,但是 JAXB 支持使用 @XmlID/@XmlIDREF 用于映射表示图形的节点之间的关系。标准机制是一键对应一个外键。 JPA 支持复合键的概念,MOXy 使用 @XmlKey@XmlJoinNodes 也支持复合键的概念(类似于 JPA 中的 @XmlJoinColumns)。

员工

@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;

}

电话号码

@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;

}

了解更多信息

 

3. MOXy 允许复合和嵌入式密钥

JPA 还可以使用嵌入式密钥类来表示复合密钥。 MOXy 也支持这种类型的复合键。

了解更多信息

4. EclipseLink JAXB (MOXy) 和 EclipseLink JPA 具有共享概念

EclipseLink 提供共享公共核心的 JAXB 和 JPA 实现。这意味着它们共享许多相同的概念,例如:

虚拟访问方法

EclipseLink 支持虚拟属性的概念。当您创建需要按租户进行自定义的多租户应用程序时,这非常有用。 EclipseLink 的 JPA 和 JAXB 实现都支持这个概念。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

Yes you can map JPA entities to XML, and the following are some ways that EclipseLink JAXB (MOXy) makes this easier.

1. Bidirectional Mappings

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

 

2. Mapping Compound Key Relationships

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

@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

 

3. MOXy allows for Composite and Embedded Keys

JPA can also use embedded key classes to represent composite keys. MOXy also supports this style of composite keys.

For More Information

 

4. EclipseLink JAXB (MOXy) and EclipseLink JPA Have Shared Concepts

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文