NPE Thrown Marshalling Entity in JAX-RS

自闭症网瘾萝莉.ら 提交于 2019-12-01 05:51:23

We have had some offline discussion about this issue, and for the benefit of others that find this post below is the correct setup of using @XmlInverseReference at multiple levels:

Entity A

import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import java.util.Set;

@Entity
@XmlRootElement
public class EntityA implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private Set<EntityB> entityBs;

    @Id
    @XmlAttribute
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(mappedBy = "entityABean")
    @XmlElement
    public Set<EntityB> getEntityBs() {
        return this.entityBs;
    }

    public void setEntityBs(Set<EntityB> entityBs) {
        this.entityBs = entityBs;
    }

}

Entity B

import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlInverseReference;

import java.util.Set;

@Entity
@XmlRootElement
public class EntityB implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private Set<EntityC> entityCs;
    private EntityA entityABean;

    @Id
    @XmlAttribute
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    @JoinColumn(name = "EntityA")
    @XmlInverseReference(mappedBy = "entityBs")
    public EntityA getEntityABean() {
        return this.entityABean;
    }

    public void setEntityABean(EntityA entityABean) {
        this.entityABean = entityABean;
    }

    @OneToMany(mappedBy = "entityBBean")
    @XmlElement
    public Set<EntityC> getEntityCs() {
        return this.entityCs;
    }

    public void setEntityCs(Set<EntityC> entityCs) {
        this.entityCs = entityCs;
    }
}

Entity C

import java.io.Serializable;
import javax.persistence.*;

import org.eclipse.persistence.oxm.annotations.XmlInverseReference;

@Entity
public class EntityC implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private EntityB entityBBean;

    @Id
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    @JoinColumn(name = "EntityB")
    @XmlInverseReference(mappedBy = "entityCs")
    public EntityB getEntityBBean() {
        return this.entityBBean;
    }

    public void setEntityBBean(EntityB entityBBean) {
        this.entityBBean = entityBBean;
    }
}

Demo

import java.io.FileInputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(EntityA.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        FileInputStream xml = new FileInputStream("src/test/jaxb/input.xml");
        EntityA a = (EntityA) unmarshaller.unmarshal(xml);

        for(EntityB b : a.getEntityBs()) {
            System.out.println(b.getEntityABean());
            for(EntityC c : b.getEntityCs()) {
                System.out.println(c.getEntityBBean());
            }
        }
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(a, System.out);
    }

}

Demo - Output

test.jaxb.EntityA@1292d26
test.jaxb.EntityB@196c1b0
test.jaxb.EntityB@196c1b0
test.jaxb.EntityA@1292d26
test.jaxb.EntityB@1e13d52
test.jaxb.EntityB@1e13d52

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<entityA>
   <entityBs>
      <entityCs/>
      <entityCs/>
   </entityBs>
   <entityBs>
      <entityCs/>
      <entityCs/>
   </entityBs>
</entityA>

This issue is also being handled on the EclipseLink forums, for more information see:

Below is another example of using @XmlInverseReference with a JPA model

Jordan Allan

Did you include a jaxb.properties file in with your entity classes?

Check out Blaise's answer to this question: JAXB Mapping cyclic references to XML

Hope this helps. RG

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!