简体   繁体   中英

JAXB and JPA, Relationships

As I am still new to using JPA (Implementation: EclipseLink) and JAXB, i encounter a Problem while thinking about the Design:

I have the following setup (simplified):

@XmlRootElement
@Entity
@Tabe(name="Packages")
class package {
 @Id
 @Column(name="p_id")
 int id;

 String content;

 @ManyToOne(fetch = FetchType.LAZY, optional=true)
 @JoinColumn(name="s_id", nullable=true, updatable=true)
 Store fk_store;
}

@XmlRootElement
@Entity
@Table(name=stores")
class Store {
 @Id
 @Column(name="s_id")
 int id;

 @OneToMany (mappedBy="fk_store", fetch=FetchType.LAZY)
 List<Package> packagesStored;
}

I figured out from several Internet resources, I have to maintain both sides of a relationship for JPA to work.

So a Store contains a List of packages, a Package contains a Store.

If i now build a RESTfull Webservice and want to send an XML representation of a Package/Warehouse, will these items then Recursively be embedded in each other?

    <package>
         <store>
              <packagesStored>
                    <package>
                         <store>
...

if yes, how do i prevent this? If not, why not? ...

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

If you also use EclipseLink as your JAXB provider then you can leverage the @XmlInverseReference extension to map the directional relationship:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM