Search code examples
jpajaxbjpa-2.0eclipselinkhyperjaxb

JPA to JAXB issue


I have entities which are created from eclipseLink.I am trying to use the same objects to get generate an xml.I have a primarykey reference inside a class and I am not sure what annotation I need to use to get the desired xml. I have

public class ABC implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private NamePK id;

@XmlElement
private String address1;

    @XmlElement
private String address1;

... }

 public class NamePK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;

@Column(name="county")
@XmlTransient
private String county;

@Column(name="NAME")
@XmlElement
private String name;

....

}

How should the annotation at the NamePk be for me to get the xml output as

 <name>XXX</name>
 <Address1>YYY</Address1>
 <Address2>ZZZ</Address2>

Do I need annotate at both the levels of the PK?(Class Level and reference)

Thanks


Solution

  • Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.

    You can use the MOXy JAXB implementation in EclipseLink to accomplish this:

    ABC

    You can use @XmlPath(".") to have the embedded ID marshal/unmarshal at the same level as your ABC class:

    public class ABC implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @EmbeddedId
        @XmlPath(".")
        private NamePK id;
    
        @XmlElement
        private String address1;
    
        @XmlElement
        private String address1;
    
    }
    

    For More Information