微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – SAXException2:在对象图中检测到循环.怎么回事?

我有一个Web服务,其中包含基于我拥有的数据库模式使用NetBeans生成Java文件.

我有时会遇到奇怪的例外,其中一个就是这个:

javax.xml.ws.WebServiceException: 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: org.mylib.Person[ personId=1 ] ->org.mylib.TeamPerson[ teamPersonPK=org.mylib.teamPersonPK[ teamId=1,personId=1 ] ] -> org.mylib.Person[ personId=1 ]]

我用google搜索了这个异常并找到了一些类似的情况,但我仍然无法理解这个问题.我刚刚使用NetBeans生成了这些类(Person.java,Team.java,TeamPerson.java),那么问题是如何发生的呢?

当我试图让所有人:

Iterator iter = team.getTeamPersonCollection().iterator();
while(iter.hasNext()) {
            Person person = ((TeamPerson)iter.next()).getPerson();
...
}

编辑
如果我从TeamPerson中删除Team引用,则会收到以下错误

Internal Exception: Exception [EclipseLink-7154] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [teamPersonCollection] in entity class [class org.mylib.Team] has a mappedBy value of [team] which does not exist in its owning entity class [org.mylib.TeamPerson]. If the owning entity class is a @MappedSuperclass,this is invalid,and your attribute should reference the correct subclass.
    at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)

编辑2
生成的类的一部分如下所示:

Team.java

public class Team implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "team_id")
    private Integer teamId;
    @Column(name = "type")
    private String type;
    @OnetoMany(cascade = CascadeType.ALL,mappedBy = "team")
    private Collection<TeamPerson> teamPersonCollection;

Person.java

public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "person_id")
    private Integer personId;
    @Column(name = "name")
    private String name;
    @OnetoMany(cascade = CascadeType.ALL,mappedBy = "person")
    private Collection<TeamPerson> teamPersonCollection;

TeamPerson.java

public class TeamPerson implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected TeamPersonPK teamPersonPK;
    @Basic(optional = false)
    @Column(name = "timestamp")
    @Temporal(TemporalType.TIMESTAMP)
    private Date timestamp;
    @JoinColumn(name = "team_id",referencedColumnName = "team_id",insertable = false,updatable = false)
    @ManyToOne(optional = false)
    private Team team;
    @JoinColumn(name = "person_id",referencedColumnName = "person_id",updatable = false)
    @ManyToOne(optional = false)
    private Person person;

TeamPersonPK.java

@Embeddable
public class TeamPersonPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "team_id")
    private int teamId;
    @Basic(optional = false)
    @Column(name = "person_id")
    private int personId;

解决方法

解决方案只是在导致循环的属性的getter中添加注释:“@XmlTransient”(javax.xml.bind.annotation.XmlTransient).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


应用场景 C端用户提交工单、工单创建完成之后、会发布一条工单创建完成的消息事件(异步消息)、MQ消费者收到消息之后、会通知各处理器处理该消息、各处理器处理完后都会发布一条将该工单写入搜索引擎的消息、最终该工单出现在搜索引擎、被工单处理人检索和处理。 事故异常体现 1、异常体现 从工单的流转记录发现、
线程类,设置有一个公共资源 package cn.org.chris.concurrent; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @Descrip
Java中的数字(带有0前缀和字符串)
在Java 9中使用JLink的目的是什么?
Java Stream API Filter(过滤器)
在Java中找到正数和负数数组元素的数量
Java 9中JShell中的不同启动脚本是什么?
使用Java的位填充错误检测技术
java中string是什么
如何使用Java中的JSON-lib API将Map转换为JSON对象?