Java SAXException2:在对象图中检测到循环。情况如何?
我有一个带有Java类文件的web服务,这些文件是根据我的数据库模式使用NetBeans生成的 我有时会遇到奇怪的异常,其中一个是:Java SAXException2:在对象图中检测到循环。情况如何?,java,web-services,sax,Java,Web Services,Sax,我有一个带有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
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中删除团队引用,则会出现以下错误:
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;
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;
这可能是因为您的
Person
类包含类型为TeamPerson
的字段,TeamPerson
包含类型为Person
的字段。而marshaller在解析这样的循环init时感到困惑
UPD。也许你需要改变这个
@OneToMany(cascade = CascadeType.ALL, mappedBy = "team")
private Collection<TeamPerson> teamPersonCollection;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“team”)
私人收藏小组个人收藏;
致:
@OneToMany(cascade=CascadeType.ALL,mappedBy=“teamId”)
私人收藏小组个人收藏;
由于类
TeamPerson
?中不存在字段team
,因此解决方案只是添加注释:“@xmltransive
”(javax.xml.bind.annotation.xmltransive
)在导致循环的属性的getter处。如果从TeamPerson中删除对Team的引用,则会出现另一个异常。见我的编辑上面。我怎样才能解决这个问题呢?我不知道,类是这样生成的,mappedBy=“team”
。不要问我为什么,但如果我更改为teamId
,我会得到一个ValidationException:在[class org.mylib.Person]和[class org.mylib.TeamPerson]之间遇到不兼容的映射当映射的基数与其反向指针的基数不一致时,通常会发生这种情况。
还有其他建议吗?该对象是返回的JSON@XmlTransient的一部分,完全排除了它。用户希望该数据成为JSONHi@Rox的一部分,您最终解决了这个问题吗?我也一样!
@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;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "team")
private Collection<TeamPerson> teamPersonCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "teamId")
private Collection<TeamPerson> teamPersonCollection;