isy*_*isy 3 xml hibernate resteasy moxy
我正在使用Hibernate和RESTeasy,我试图避免与这些实体的循环,因为我在Artiste和Oeuvre实体之间有一个OneToMany(ManyToOne)双向关系:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我决定使用MOXy,
这是我的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>
Run Code Online (Sandbox Code Playgroud)
nb:我想只有org.eclipse.persistence.moxy-2.3.2.jar,因为我正在使用hibernate(我不想要eclipseLink),但我还有3个其他的jar(包括核心)
然后我将jaxb.properties文件放在我的实体的包中:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Run Code Online (Sandbox Code Playgroud)
并将@XmlInverseReference(mappedBy ="oeuvres")添加到getArtiste()而不是@XmlTranscient ==>我没有循环(如xmlTranscient)但我仍然没有任何后退指针.
然后我添加了@XmlID和@XmlIDREF,艺术家的id现在在艺术作品的xml结果中表示但它没有好的价值(0但是应该是别的东西)
<Oeuvre>
<hasBeenReproduced>false</hasBeenReproduced>
<artiste>0</artiste>
<year>2010</year>
<id>2</id>
<titre>La joconde</titre>
</Oeuvre>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?Thx提前
编辑:
好吧,当我编组一个"Artiste"对象时,我使用@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>
Run Code Online (Sandbox Code Playgroud)
根据您的示例,这是正确的行为.因此,如果我理解得很好,就不可能在"全部"输出(上面给出)中引用艺术家ID.我们无法从艺术品中找回艺术家.在我的情况下,我不必使用@XmlID?
谢谢你的完整答案Blaise Doughan,非常感谢
注意: 我是EclipseLink JAXB(MOXy)的负责人,也是JAXB(JSR-222)专家组的成员.
下面是如何使用MOXy @XmlInverseReference扩展的简化示例.
富
Foo 是此示例的根对象.
package forum13957068;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
String fooProp;
@XmlElement(name="bar")
List<Bar> bars;
}
Run Code Online (Sandbox Code Playgroud)
酒吧
Bar是一个子对象.我们使用@XmlInverseReference注释来填充foo字段,引用父对象.
package forum13957068;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {
private String barProp;
@XmlInverseReference(mappedBy="bars")
Foo foo;
}
Run Code Online (Sandbox Code Playgroud)
jaxb.properties
要将MOXy指定为JAXB提供程序,您需要包含一个jaxb.properties使用以下条目调用的文件:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Run Code Online (Sandbox Code Playgroud)
input.xml中
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<fooProp>A</fooProp>
<bar>
<barProp>B</barProp>
</bar>
<bar>
<barProp>B</barProp>
</bar>
</foo>
Run Code Online (Sandbox Code Playgroud)
演示
以下演示代码将解组XML,检查后退指针,然后将对象封送回XML.
package forum13957068;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum13957068/input.xml");
Foo foo = (Foo) unmarshaller.unmarshal(xml);
for(Bar bar : foo.bars) {
System.out.println(bar.foo == foo);
}
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
Run Code Online (Sandbox Code Playgroud)
产量
以下是运行演示代码的输出.节点如何正确填充后向指针.
true
true
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<fooProp>A</fooProp>
<bar>
<barProp>B</barProp>
</bar>
<bar>
<barProp>C</barProp>
</bar>
</foo>
Run Code Online (Sandbox Code Playgroud)
MOXy和Maven
您可以在pom.xml文件中使用以下内容来拉入MOXy.
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>EclipseLink Repo</id>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>
</repositories>
Run Code Online (Sandbox Code Playgroud)
对于一些拉入MOXy的POM文件,请查看我在GitHub上托管的示例:
UPDATE
根据您的示例,这是正确的行为.因此,如果我理解得很好,就不可能在"全部"输出(上面给出)中引用艺术家ID.我们无法从艺术品中找回艺术家.在我的情况下,我不必使用@XmlID?
下面我提供了一个使用@XmlID/ @XmlIDREF代替的替代映射@XmlInverseReference.
富
package forum13957068;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlID
String id;
String fooProp;
@XmlElement(name="bar")
List<Bar> bars;
}
Run Code Online (Sandbox Code Playgroud)
酒吧
package forum13957068;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {
private String barProp;
@XmlIDREF
Foo foo;
}
Run Code Online (Sandbox Code Playgroud)
input.xml中
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<id>1</id>
<fooProp>A</fooProp>
<bar>
<foo>1</foo>
<barProp>B</barProp>
</bar>
<bar>
<foo>1</foo>
<barProp>B</barProp>
</bar>
</foo>
Run Code Online (Sandbox Code Playgroud)
欲获得更多信息