2012-12-19 175 views
3

我使用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?

你完整的答案布莱斯Doughan

THX,它是非常赞赏

+0

我很乐意提供帮助。我已经更新了我的答案,包括一个@ @ XmlID' /'@ XmlIDREF'映射,如果这样可以更好地匹配您的用例:http://stackoverflow.com/a/13960663/383861 –

回答

6

注:我是EclipseLink JAXB (MOXy)铅和JAXB (JSR-222)专家小组的成员。

下面是如何使用莫西的@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

要指定莫西为您的JAXB提供者包括一个名为jaxb.properties具有以下项文件:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

的input.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> 

莫西和Maven

您可以使用您pom.xml文件中的以下的莫西拉。

<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> 

对于拉莫西检查出我已经在GitHub上托管的例子一些例子POM文件:


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; 

} 

酒吧

package forum13957068; 

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class Bar { 

    private String barProp; 

    @XmlIDREF 
    Foo foo; 

} 

的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> 

佛[R更多信息

+0

非常感谢您的回答,我有更新了我的帖子 – isy