Ecl*_*ica 3 java postgresql jpa eclipselink
我正在开发一个EclipseLink项目,其中一个用户可以"关注"另一个用户,这可以在社交媒体网站上完成.我设置了一个User
实体(引用一个名为的表users
),其中包含一个"关注者"列表(跟随该用户的用户)和另一个"关注"列表(用户正在关注的用户).该关系在一个单独的表中定义,该表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;
}
}
Run Code Online (Sandbox Code Playgroud)
该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)
...
Run Code Online (Sandbox Code Playgroud)
如果我应该提供更多的堆栈跟踪,请告诉我.任何提示?
每次你有 @OneToMany
(一个集合)你需要添加@JsonIgnore
它,否则它将导致一个无限循环,导致堆栈溢出异常,因为它一直在父(一方)和孩子(多方)之间查找有关处理此类问题的更多信息,请查看这篇优秀的文章http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
归档时间: |
|
查看次数: |
6643 次 |
最近记录: |