我使用Hibernate和RestEasy的,我会尽量避免与这些实体一个周期,因为我有一个一对多(多对一)艺人及全部作品实体之间的关系bidirectionnal:使用莫西避免循环,@XmlInverseReference @XmlID
Oeuvre.java
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@XmlRootElement(name = "oeuvre")
public abstract class Oeuvre {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Embedded
private Dimension dimension;
@XmlElement(defaultValue = "true")
private boolean hasBeenReproduced;
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinColumn(name = "artiste_id")
@XmlIDREF
private Artiste artiste;
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// @XmlTransient
@XmlInverseReference(mappedBy = "oeuvres")
public Artiste getArtiste() {
return artiste;
}
public void setArtiste(Artiste artiste) {
this.artiste = artiste;
artiste.addOeuvre(this);
}
}
Personne.java
import javax.persistence.*;
import javax.xml.bind.annotation.XmlID;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Personne {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@XmlID
private int id;
}
Artiste.java
import java.util.*;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
@Entity
@XmlRootElement(name = "artiste")
public class Artiste extends Personne {
private String bibliographie;
@OneToMany(mappedBy = "artiste", orphanRemoval = true, cascade = {
CascadeType.PERSIST, CascadeType.REMOVE })
private List<Oeuvre> oeuvres = new ArrayList<Oeuvre>();
@XmlElement
public List<Oeuvre> getOeuvres() {
return oeuvres;
}
public void setOeuvres(List<Oeuvre> oeuvres) {
this.oeuvres = oeuvres;
}
}
所以我决定用莫西,
这里是我的POM
<repository>
<id>EclipseLink</id>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy </artifactId>
<version>2.3.2</version>
</dependency>
注:我想只有org.eclipse.persistence.moxy -2.3.2.jar由于我使用Hibernate(我不想要的EclipseLink),但我也有其他3罐(包括核心)
然后我把jaxb.prop在包我的实体ERTIES文件:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
并补充@XmlInverseReference(的mappedBy = “小菜”),以getArtiste(),而不是OD @XmlTranscient ==>我没有再循环(如xmlTranscient)但我仍然没有任何回指针。
然后我说@XmlID & @XmlIDREF,艺术家的ID现在已经代表了艺术作品的XML结果,但它不具有良好的值(0,但768,16是别的东西)
<Oeuvre>
<hasBeenReproduced>false</hasBeenReproduced>
<artiste>0</artiste>
<year>2010</year>
<id>2</id>
<titre>La joconde</titre>
</Oeuvre>
我到底做错了什么? THX提前
编辑:
好吧,我有使用@XmlInverseReference以下输出时,我的马歇尔“艺人”对象:
<artiste>
<id>1</id>
<nom>a</nom>
<prenom>b</prenom>
<oeuvres>
<hasBeenReproduced>false</hasBeenReproduced>
<year>2010</year>
<id>25</id>
<titre>La joconde</titre>
</oeuvres>
</artiste>
根据您的例子,这是正确的行为。 所以,如果我理解得很好,就不可能在“作品”输出(上面给出)中引用artiste id。我们无法从艺术品中找回艺术家。 在我的情况下,我不必使用@XmlID?
你完整的答案布莱斯DoughanTHX,它是非常赞赏
我很乐意提供帮助。我已经更新了我的答案,包括一个@ @ XmlID' /'@ XmlIDREF'映射,如果这样可以更好地匹配您的用例:http://stackoverflow.com/a/13960663/383861 –