我正在尝试使用平针织物,弹簧和休眠来开发应用程序。我希望有人可以帮我解决这个问题。 当我尝试从数据库中提取Object并将其转换为xml元素时,我得到了这个异常。
[com.sun.istack.internal.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: ma.noyauSolution.entities.Region@22b8be54 -> ma.noyauSolution.entities.Province@38677a45 -> ma.noyauSolution.entities.Region@22b8be54]
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:159)
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:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: ma.noyauSolution.entities.Region@22b8be54 -> ma.noyauSolution.entities.Province@38677a45 -> ma.noyauSolution.entities.Region@22b8be54]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:179)
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:157)
... 27 more
Caused by: com.sun.istack.internal.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: ma.noyauSolution.entities.Region@22b8be54 -> ma.noyauSolution.entities.Province@38677a45 -> ma.noyauSolution.entities.Region@22b8be54
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.pushObject(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)
... 32 more
这是我的豆类地区和省份:
@XmlRootElement
@Entity
@Table(name="T_REGION")
public class Region implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idRegion;
@Column(name = "NOM_REGION", nullable = false, length = 30, unique = true)
private String nomRegion;
@OneToMany(fetch = FetchType.EAGER,mappedBy = "region", cascade = CascadeType.ALL)
@XmlTransient
private List<Province> provinces = new ArrayList<Province>();
@OneToMany(fetch = FetchType.EAGER,mappedBy = "region")
@XmlTransient
private List<ActualiteRegion> actualites = new ArrayList<ActualiteRegion>();
public Long getIdRegion() {
return idRegion;
}
public void setIdRegion(Long idRegion) {
this.idRegion = idRegion;
}
public String getNomRegion() {
return nomRegion;
}
public void setNomRegion(String nomRegion) {
this.nomRegion = nomRegion;
}
public Region(String nomRegion) {
this.nomRegion = nomRegion;
}
public Region() {
}
public List<Province> getProvinces() {
return provinces;
}
public void setProvinces(List<Province> provinces) {
this.provinces = provinces;
}
public List<ActualiteRegion> getActualites() {
return actualites;
}
public void setActualites(List<ActualiteRegion> actualites) {
this.actualites = actualites;
}
}
@Entity
@Table(name="T_PROVINCE")
public class Province implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idProvince;
@Column(name = "NOM_PROVINCE", nullable = false, length = 20, unique = true)
private String nomProvince;
@ManyToOne(cascade = javax.persistence.CascadeType.ALL)
@JoinColumn(name = "IDREGION")
@XmlInverseReference(mappedBy="provinces")
private Region region;
@OneToMany(fetch=FetchType.EAGER,mappedBy = "province")
@XmlTransient
private List<Ville> villes = new ArrayList<Ville>();
public Long getIdProvince() {
return idProvince;
}
public void setIdProvince(Long idProvince) {
this.idProvince = idProvince;
}
public String getNomProvince() {
return nomProvince;
}
public void setNomProvince(String nomProvince) {
this.nomProvince = nomProvince;
}
public Province(String nomProvince) {
this.nomProvince = nomProvince;
}
public Province() {
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
public List<Ville> getVilles() {
return villes;
}
public void setVilles(List<Ville> villes) {
this.villes = villes;
}
}
这是我的类RegionService:
@Component
@Path("/regions")
public class RegionServiceImpl implements IRegionService {
private IRegionDAO dao;
public void setDao(IRegionDAO dao) {
this.dao = dao;
}
public void addRegion(Region r) {
dao.addRegion(r);
}
@Path("/{id}")
@GET
@Produces(value=MediaType.APPLICATION_XML)
public Region getRegionById(@PathParam(value = "id")Long idAb) {
return dao.getRegionById(idAb);
}
public void deleteRegion(Region r) {
dao.deleteRegion(r);
}
public void updateRegion(Region r) {
dao.updateRegion(r);
}
public List<Region> getAllRegions() {
return dao.getAllRegions();
}
}