我收到此错误
The server encountered an internal error (javax.xml.bind.MarshalException - with linked
exception: [com.sun.istack.SAXException2: A cycle is detected in the object graph. This
will cause infinitely deep XML: com.app.Vote@16cc0f7 ->
com.app.ComplicatedVote@1a961d2 -> com.app.Vote@16cc0f7]) that
prevented it from fulfilling this request.
这是两个类(至少是重要部分)
复杂投票.java
@XmlRootElement(name="complicated-vote")
@Entity
@Table(name="complicated_votes")
public class ComplicatedVote implements Serializable{
private static final long serialVersionUID = 1L;
@XmlElement(required=true)
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable=false)
private int id;
@XmlElement(required=true)
@Column(name="name", nullable=false)
private String name;
@XmlTransient
@OneToMany(cascade = {CascadeType.ALL}, targetEntity=Vote.class, fetch=FetchType.EAGER)
@JoinTable(
name="PARENT_CHILD",
joinColumns = @JoinColumn( name="PARENT_ID"),
inverseJoinColumns = @JoinColumn( name="CHILD_ID")
)
private List<Vote> children = new ArrayList<Vote>();
投票.java
@XmlRootElement(name="vote")
@Entity
@Table(name="votes")
public class Vote implements Serializable {
private static final long serialVersionUID = 1L;
@XmlElement(required=true)
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable=false)
private int id;
@XmlElement(required=true)
@Column(name="name", nullable=false)
private String name;
@XmlElement(required=true)
@Column(name="max", nullable=false)
private int max;
@XmlElement(required=true)
@Column(name="current", nullable=false)
private int current;
@XmlElement(required=true)
@Column(name="is_simple", nullable=false)
private int isSimple;
@XmlTransient
@ManyToOne (cascade = {CascadeType.ALL}, targetEntity=ComplicatedVote.class, fetch=FetchType.EAGER)
private ComplicatedVote parent;
@XmlElement(required=true)
@Transient
private int votedOn;
@XmlTransient
@ManyToMany(cascade = {CascadeType.ALL}, targetEntity=User.class, fetch=FetchType.EAGER)
@JoinTable(name = "USER_VOTES",
joinColumns = {@JoinColumn(name = "VOTE_ID")},
inverseJoinColumns = {@JoinColumn(name = "USER_ID")}
)
private Set<User> voters = new HashSet<User>();
现在我将它与 Jax-RS 一起使用。因此,仅在尝试将其转换为 xml 时才会出现此问题。逻辑本身一切正常。问题是我不知道如何不导致该异常,因为从技术上讲逻辑没有错。或者也许是。希望有人可以阐明它为什么这样做以及如何避免它。我还应该提到,由于我使用的是@XmlTransient
,因此不应发生此错误,因为我猜它不应该编组它。那么对于这些类型的关系,我必须使用不同的注释吗?