【发布时间】:2015-12-16 05:20:34
【问题描述】:
由于多对多关系的循环引用而出现以下错误。
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError)
我尝试过使用 @JsonIdentityInfo, @com.fasterxml.jackson.annotation.JsonIgnore 和 @JsonManagedReference,但这些都不适合我。我正在使用 lombok,因此使用了 @getter/setter。
用户表
@Entity
@Table(name = "user")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="user_id", scope=User.class)
public class User implements Serializable{
@Getter
@Setter
@Id
@GeneratedValue(strategy = IDENTITY, generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
public int user_id;
@JsonManagedReference
@Getter
@Setter
@com.fasterxml.jackson.annotation.JsonIgnore
@ElementCollection(targetClass=UserInterest.class)
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.user", cascade=CascadeType.ALL)
public Set<UserInterest> userInterestSet = new HashSet<UserInterest>();
}
利息表
@Entity
@Table(name = "interest")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="interest_id", scope=Interest.class)
public class Interest implements Serializable{
@Getter
@Setter
@Id
@GeneratedValue(strategy = IDENTITY, generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
public int interest_id;
@Getter
@Setter
@Column(name = "interest_name", nullable = false, length=100)
public String interest_name;
@Getter
@Setter
@com.fasterxml.jackson.annotation.JsonIgnore
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.interest", cascade=CascadeType.ALL)
public Set<UserInterest> userInterestSet = new HashSet<UserInterest>();
}
UserInterest 连接表
@Entity
@Table(name="user_interest")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="pk", scope=UserInterest.class)
@AssociationOverrides({
@AssociationOverride(name="pk.user", joinColumns=@JoinColumn(name="user_id")),
@AssociationOverride(name="pk.interest", joinColumns=@JoinColumn(name="interest_id"))
})
public class UserInterest implements Serializable {
@Getter
@Setter
@com.fasterxml.jackson.annotation.JsonIgnore
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
public int user_interest_id;
@EmbeddedId
@Getter
@Setter
@com.fasterxml.jackson.annotation.JsonIgnore
public UserInterestId pk;
@Transient
@Getter
@Setter
@com.fasterxml.jackson.annotation.JsonIgnore
public User user;
@Transient
@Getter
@Setter
@com.fasterxml.jackson.annotation.JsonIgnore
public Interest interest;
}
UserInterestId 类
@Embeddable
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=UserInterestId.class)
public class UserInterestId implements Serializable{
@Getter
@Setter
@org.codehaus.jackson.annotate.JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@com.fasterxml.jackson.annotation.JsonIgnore
@JsonBackReference
public User user;
@Getter
@Setter
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="interest_id")
@ManyToOne(fetch = FetchType.LAZY)
@com.fasterxml.jackson.annotation.JsonIgnore
@JsonBackReference
public Interest interest;
}
当我在加入的用户对象上运行以下命令时,我得到了错误:
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
ow.writeValueAsString(userSet);
我对 Hibernate 还是很陌生,所以希望得到任何指点。我已经阅读了 5-10 篇建议我上面提到的解决方案的 SO 文章,但我仍然无法停止循环引用。非常感谢!
【问题讨论】:
标签: java mysql json hibernate recursion