我正在开发一个EclipseLink项目,其中一个用户可以像社交媒体网站上一样“关注”另一个用户。我已经使用一个名为“User”的实体(引用了一个名为“users”的表)来设置这个功能,该实体具有“followers”列表(关注该用户的用户)和“following”列表(该用户正在关注哪些用户)。关系在一个名为“followers”的单独表中定义,该表包含已关注用户的ID(“user_id”)和已关注用户的ID(“follower_id”)的列。
我的用户模型如下所示:
@Entity
@Table(name = "users")
@NamedQuery(name = "User.findAll", query = "SELECT u FROM USER u")
public class User {
// other attributes
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "follower", joinColumns = @JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(
name = "follower_id", referencedColumnName = "id"))
private List<User> followers;
@ManyToMany(mappedBy = "followers")
private List<User> following;
// other getters and setters
public List<User> getFollowers() {
return this.followers;
}
public List<User> getFollowing() {
return this.following;
}
}
getFollowers()
方法似乎正常工作,但当调用getFollowing()
时,我得到一堆控制台垃圾信息,最终导致StackOverflowException:
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion
(StackOverflowError) (through reference chain:
org.eclipse.persistence.indirection.IndirectList[0]-
>org.myproject.model.User["followers"]-
>org.eclipse.persistence.indirection.IndirectList[0]-
>org.myproject.model.User["following"]-
...
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase
.serializeFields(BeanSerializerBase.java:518)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize
(BeanSerializer.java:117)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer
.serializeContents(IndexedListSerializer.java:94)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer
.serializeContents(IndexedListSerializer.java:21)
...
请告诉我是否应该提供更多的堆栈跟踪信息。有什么提示吗?
@JsonIgnore
)。你真是救星! - Ecliptica