6

我有一个带有 Java 类文件的 Web 服务,这些文件是基于我拥有的数据库模式使用 NetBeans 生成的。

我有时会遇到奇怪的异常,其中之一就是:

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 ]]

我用谷歌搜索了这个异常并发现了一些类似的情况,但我仍然无法理解这个问题。我刚刚使用 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
部分生成的类如下所示:

团队.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;

人.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", insertable = false, 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;
4

2 回答 2

10

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

于 2013-02-08T21:14:54.240 回答
0

好吧,也许那是因为您的Person类包含 type 的字段TeamPerson并且TeamPerson包含 type 的字段Person。并且编组器在解析这样的循环初始化时感到困惑?

UPD。也许你需要改变这个

@OneToMany(cascade = CascadeType.ALL, mappedBy = "team")
    private Collection<TeamPerson> teamPersonCollection;

至:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "teamId")
    private Collection<TeamPerson> teamPersonCollection;

因为team类中不存在字段TeamPerson

于 2012-05-14T08:22:09.247 回答