- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 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);
}
}
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;
}
}
所以我决定使用 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>
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
并且将 @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>
我做错了什么?提前致谢
编辑:
好的,当我编码“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>
根据您的示例,这是正确的行为。因此,如果我理解得很好,就不可能在“Oeuvre”输出(上面给出)中引用艺人 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;
}
栏
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;
}
jaxb.properties
要将 MOXy 指定为您的 JAXB 提供程序,您需要包含一个名为 jaxb.properties
的文件,其中包含以下条目:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
输入.xml
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<fooProp>A</fooProp>
<bar>
<barProp>B</barProp>
</bar>
<bar>
<barProp>B</barProp>
</bar>
</foo>
演示
以下演示代码将解码 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);
}
}
输出
下面是运行演示代码的输出。了解如何正确填充后向指针。
true
true
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<fooProp>A</fooProp>
<bar>
<barProp>B</barProp>
</bar>
<bar>
<barProp>C</barProp>
</bar>
</foo>
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>
有关引入 MOXy 的一些示例 POM 文件,请查看我在 GitHub 上托管的示例:
更新
According to your example this is the correct behaviour. So if i understand well, it's not possible to have a reference of the artiste id in the "Oeuvre" output (given above). We can't retrieve the artist from a work of art. In my case i don't have to use @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;
}
栏
package forum13957068;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {
private String barProp;
@XmlIDREF
Foo foo;
}
输入.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>
了解更多信息
关于xml - 使用 MOXy 避免循环,@XmlInverseReference @XmlID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13957068/
我发现了类似的问题,但没有一个答案对我有帮助。 所以,我有 2 个具有反向指针关系的对象。家长: @XmlRootElement public class A { private B b;
我正在使用 MOXy JAXB 实现并广泛使用 @XmlInverseReference 注释。但是,我最近遇到了这种方法似乎不起作用的情况。如果我有一个类包含一个字段,该字段具有与父类类型相同的属性
我正在关注 http://blog.bdoughan.com/2010/08/creating-restful-web-service-part-35.html 上的教程。我将文件复制并粘贴到 net
我正在使用 Hibernate 和 RESTeasy,我尽量避免与这些实体形成循环,因为我在 Artiste 和 Oeuvre 实体之间存在 OneToMany (ManyToOne) 双向关系: O
我是一名优秀的程序员,十分优秀!