1
votes

Ok i have been trying this for days on end but cant seem to figure what is wrong. The basic idea here is that i am trying to create a restful web service that would display properties of of two entity beans having multiplicity bidirectional relationship with each other. Now i want to use moxy implementation of JAXB to take advantage of @XmlInverseReference annotation to maintain referential integrity, but keep coming up with exceptions. below is an except from my classes i am using glassfish 3.1.1 and moxy eclipselink 2.3.0

Passenger Class:

package kharesoft.sita;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
import javax.xml.bind.annotation.*;


@XmlRootElement
@XmlType(propOrder = {"id", "firstName", "lastName", "boardingNo", "seatNo", "flightNo", "dateOfFlight", "bags"})
@Entity
@NamedQuery(name = "findPaxByBagTag", query = "select p from Passenger p,IN (p.bags) b where b.bagTagNo=:bagTagNo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Passenger implements Serializable {

    @XmlElement
    private String firstName;
    @XmlElement
    private String lastName;
    @XmlElement
    private String boardingNo;
    @XmlElement
    private String seatNo;
    @XmlElement
    private String flightNo;
    @XmlElement
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateOfFlight;
    @XmlElementWrapper  
    @XmlElements({
    @XmlElement(name = "bagtag")})
    @OneToMany(mappedBy = "passenger")
    private Set<Bag> bags;
    private static final long serialVersionUID = 1L;
    @XmlElement
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Set<Bag> getBags() {
        return bags;
    }

    public void setBags(Set<Bag> bags) {
        this.bags = bags;
    }

    //Remaining getters and setters not shown

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Passenger)) {
            return false;
        }
        Passenger other = (Passenger) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "kharesoft.sita.Passenger[ id=" + id + " ]";
    }
}


Bag Class:


package kharesoft.sita;

import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;


@XmlRootElement
@Entity
@NamedQuery(name = "findBagWithTagNo", query = "select b from Bag b where b.bagTagNo=:bagTagNo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Bag implements Serializable {

    private static final long serialVersionUID = 1L;
    @XmlElement
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @XmlElement
    private String bagTagNo;
    @XmlElement
    @ManyToOne
    private Passenger passenger;


    //other getters and setters not shown

    @XmlInverseReference(mappedBy = "bags")
    public Passenger getPassenger() {
        return passenger;
    }

    public void setPassenger(Passenger passenger) {
        this.passenger = passenger;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Bag)) {
            return false;
        }
        Bag other = (Bag) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {        
        return bagTagNo;
    }
}

Exception:

SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException: Exception Description: Duplicate Property named [passenger] found on class [kharesoft.sita.Bag] - with linked exception: [Exception [EclipseLink-50072] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.JAXBException Exception Description: Duplicate Property named [passenger] found on class [kharesoft.sita.Bag]] at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.writeTo(AbstractListElementProvider.java:183) at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:722) Caused by: javax.xml.bind.JAXBException: Exception Description: Duplicate Property named [passenger] found on class [kharesoft.sita.Bag] - with linked exception: [Exception [EclipseLink-50072] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.JAXBException Exception Description: Duplicate Property named [passenger] found on class [kharesoft.sita.Bag]] at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:825) at org.eclipse.persistence.jaxb.JAXBContext.(JAXBContext.java:136) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:142) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:129) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:93) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:83) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:263) at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:250) at javax.xml.bind.ContextFinder.find(ContextFinder.java:400) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:652) at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:599) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getStoredJAXBContext(AbstractJAXBProvider.java:189) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getJAXBContext(AbstractJAXBProvider.java:182) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getMarshaller(AbstractJAXBProvider.java:160) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getMarshaller(AbstractJAXBProvider.java:139) at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.writeTo(AbstractListElementProvider.java:175) ... 33 more Caused by: Exception [EclipseLink-50072] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.JAXBException Exception Description: Duplicate Property named [passenger] found on class [kharesoft.sita.Bag] at org.eclipse.persistence.exceptions.JAXBException.duplicatePropertyName(JAXBException.java:943) at org.eclipse.persistence.jaxb.compiler.TypeInfo.addProperty(TypeInfo.java:265) at org.eclipse.persistence.jaxb.compiler.TypeInfo.setProperties(TypeInfo.java:292) at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.buildTypeInfo(AnnotationsProcessor.java:677) at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.postBuildTypeInfo(AnnotationsProcessor.java:591) at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.buildNewTypeInfo(AnnotationsProcessor.java:4050) at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processPropertyTypes(AnnotationsProcessor.java:858) at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processClassesAndProperties(AnnotationsProcessor.java:233) at org.eclipse.persistence.jaxb.compiler.Generator.(Generator.java:147) at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:822) ... 52 more

Your screen elements are hidden from view. Press Esc or move pointer to the center of the screen to return to Mail. Press Esc or move pointer here to return to Mail.by on  by WineSo fresh and so clean. AllPhotoColor

1

1 Answers

0
votes

Since you have specified @XmlAccessorType(XmlAccessType.FIELD) on the Bag class you should move the @XmlInverseReference annotation to the field. Since it is on the property MOXy thinks you want to map both the field and property and is complaining because they both have the same name.