Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用MOXy避免循环@XmlInverseReference@XmlID_Xml_Hibernate_Resteasy_Moxy - Fatal编程技术网

使用MOXy避免循环@XmlInverseReference@XmlID

使用MOXy避免循环@XmlInverseReference@XmlID,xml,hibernate,resteasy,moxy,Xml,Hibernate,Resteasy,Moxy,我正在使用Hibernate和RESTeasy,我试图避免这些实体的循环,因为我在艺人和作品实体之间有一个单体(多体)双向关系: javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 开篇作品.java import javax.persistence.*; 导入javax.xml.bind.annotation.*; 导入org.eclipse.persistence.oxm.annota

我正在使用Hibernate和RESTeasy,我试图避免这些实体的循环,因为我在艺人和作品实体之间有一个单体(多体)双向关系:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
开篇作品.java

import javax.persistence.*;
导入javax.xml.bind.annotation.*;
导入org.eclipse.persistence.oxm.annotations.xmlInversereReference;
@实体
@继承(策略=InheritanceType.SINGLE_表)
@XmlRootElement(name=“oeuvre”)
公共抽象类作品{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有int-id;
@嵌入
私人维度;
@XmlElement(defaultValue=“true”)
已重新生成私有布尔值;
@manytone(cascade=CascadeType.PERSIST,fetch=FetchType.LAZY)
@JoinColumn(name=“艺人id”)
@XmlIDREF
私人艺人;
@XmlElement
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
//@XmlTransient
@XmlInverseReference(mappedBy=“开篇作品”)
公共艺人{
回归艺人;
}
公共空间演艺人员(演艺人员){
this.artiste=艺人;
艺人。配乐(本);
}
}
Personne.java

import javax.persistence.*;
导入javax.xml.bind.annotation.XmlID;
@实体
@继承(策略=继承类型。每个类的表)
公共抽象类人员{
@身份证
@GeneratedValue(策略=GenerationType.TABLE)
@XmlID
私有int-id;
}
Artiste.java

import java.util.*;
导入javax.persistence.*;
导入javax.xml.bind.annotation.*;
@实体
@XmlRootElement(name=“artiste”)
公共级艺人扩展人员{
私有字符串书目;
@OneToMany(mappedBy=“artiste”,orphan=true,cascade={
CascadeType.PERSIST,CascadeType.REMOVE})
私有列表作品=新的ArrayList();
@XmlElement
公共列表getOeuvres(){
返回作品;
}
公共无效集合作品(列表作品){
这个。开胃菜=开胃菜;
}
}
所以我决定用MOXy

这是我的POM


日食
http://download.eclipse.org/rt/eclipselink/maven.repo
org.eclipse.persistence
org.eclipse.persistence.moxy
2.3.2     
注意:我只想要org.eclipse.persistence.moxy-2.3.2.jar,因为我正在使用hibernate(我不想要eclipseLink),但我还有3个其他jar(包括核心)

然后,我将jaxb.properties文件放入实体包中:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
并在getArtiste()中添加了@xmlInversereReference(mappedBy=“oeuvres”),而不是od@XmlTranscient ==>我不再有循环(比如XMLTransClient),但我仍然没有任何返回指针

然后我添加了@XmlID&@XmlIDREF,艺术家的id现在在一件艺术品的xml结果中表示,但它没有好的值(0但应该是其他值)


假的
0
2010
2.
蒙娜丽莎
我做错了什么?提前Thx

编辑:

好的,当我处理一个“Artister”对象时,我使用@XmlInverseReference有以下输出:


1.
A.
B
假的
2010
25
蒙娜丽莎
根据你的例子,这是正确的行为。 因此,如果我理解得很好,就不可能在“作品集”输出中引用艺人id(如上所述)。我们无法从艺术品中找回这位艺术家。 就我而言,我不必使用@XmlID


感谢Blaise Doughan的完整回答。非常感谢。

注意:我是专家组的负责人和成员

下面是一个如何使用MOXy的
@XmlInverseReference
扩展的简化示例

Foo

Foo
是本例的根对象

用于UM13957068的包;
导入java.util.List;
导入javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公开课Foo{
字符串fooProp;
@xmlement(name=“bar”)
列表栏;
}

Bar
是一个子对象。我们使用
@XmlInverseReference
注释填充
foo
字段,其中包含对父对象的引用

用于UM13957068的包;
导入javax.xml.bind.annotation.*;
导入org.eclipse.persistence.oxm.annotations.xmlInversereReference;
@XmlAccessorType(XmlAccessType.FIELD)
公共类酒吧{
私有字符串barProp;
@XmlInverseReference(mappedBy=“bar”)
富富,;
}
jaxb.properties

要将MOXy指定为JAXB提供程序,您需要包含一个名为
JAXB.properties
的文件,其中包含以下条目:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
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>

A.
B
B
演示

下面的演示代码将解组XML,检查返回指针,然后将对象封送回XML

用于UM13957068的包;
导入java.io.File;
导入javax.xml.bind.*;
公开课演示{
公共静态void main(字符串[]args)引发异常{
JAXBContext jc=JAXBContext.newInstance(Foo.class);
Unmarshaller Unmarshaller=jc.createUnmarshaller();
File xml=新文件(“src/forum13957068/input.xml”);
Foo Foo=(Foo)unmarshaller.unmarshal(xml);
适用于(酒吧:食品酒吧){
System.out.println(bar.foo==foo);
}
Marshaller=jc.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
marshaller.marshall(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的POM文件示例,请查看我的示例