好的,我连续几天都在尝试这个,但似乎无法弄清楚是什么问题。 这里的基本想法是,我正在尝试创建一个显示属性的restful Web服务 两个实体bean彼此具有多重双向关系。现在我想使用moxy实现 JAXB利用@XmlInverseReference注释来维护引用完整性,但不断提出异常。以下是我班级以外的 我正在使用glassfish 3.1.1和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;
}
}
例外:
SEVERE:映射异常响应:500(内部服务器错误) javax.ws.rs.WebApplicationException:javax.xml.bind.JAXBException: 异常描述:在类[kharesoft.sita.Bag]上找到名为[passenger]的重复属性 - 链接异常: [Exception [EclipseLink-50072](Eclipse Persistence Services - 2.3.0.v20110604-r9504):org.eclipse.persistence.exceptions.JAXBException 例外描述:在类[kharesoft.sita.Bag]上找到名为[passenger]的重复属性] 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) 在com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) 在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) 在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 在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) 在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) 在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) 在com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) 在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) 在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 在com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 在com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 在com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532) 在com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513) 在java.lang.Thread.run(Thread.java:722) 引起:javax.xml.bind.JAXBException: 异常描述:在类[kharesoft.sita.Bag]上找到名为[passenger]的重复属性 - 链接异常: [Exception [EclipseLink-50072](Eclipse Persistence Services - 2.3.0.v20110604-r9504):org.eclipse.persistence.exceptions.JAXBException 例外描述:在类[kharesoft.sita.Bag]上找到名为[passenger]的重复属性] at org.eclipse.persistence.jaxb.JAXBContext $ TypeMappingInfoInput.createContextState(JAXBContext.java:825) 在org.eclipse.persistence.jaxb.JAXBContext。(JAXBContext.java:136) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:142) 在org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:129) at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:93) 在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) 在java.lang.reflect.Method.invoke(Method.java:601) 在javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:263) 在javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:250) 在javax.xml.bind.ContextFinder.find(ContextFinder.java:400) 在javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:652) 在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) 在com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getMarshaller(AbstractJAXBProvider.java:160) 在com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getMarshaller(AbstractJAXBProvider.java:139) 在com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.writeTo(AbstractListElementProvider.java:175) ......还有33个 引起:异常[EclipseLink-50072](Eclipse Persistence Services - 2.3.0.v20110604-r9504):org.eclipse.persistence.exceptions.JAXBException 异常描述:在类[kharesoft.sita.Bag]上找到名为[passenger]的重复属性 at org.eclipse.persistence.exceptions.JAXBException.duplicatePropertyName(JAXBException.java:943) at org.eclipse.persistence.jaxb.compiler.TypeInfo.addProperty(TypeInfo.java:265) 在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) 在org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processPropertyTypes(AnnotationsProcessor.java:858) 在org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processClassesAndProperties(AnnotationsProcessor.java:233) 在org.eclipse.persistence.jaxb.compiler.Generator。(Generator.java:147) at org.eclipse.persistence.jaxb.JAXBContext $ TypeMappingInfoInput.createContextState(JAXBContext.java:822) ... 52更多
您的屏幕元素在视图中隐藏。按Esc或将指针移动到屏幕中心以返回Mail。 按Esc键或在此处移动指针返回到Mail上的Mail.falby 通过 WineSo清新干净。 AllPhotoColor
答案 0 :(得分:0)
由于您已在@XmlAccessorType(XmlAccessType.FIELD)
课程中指定了Bag
,因此您应将@XmlInverseReference
注释移至该字段。因为MOXy认为你想要映射字段和属性,并且抱怨因为它们都有相同的名称。