JPA多对多关系导致无限递归和堆栈溢出错误 [英] JPA many-to-many relationship causing infinite recursion and stack overflow error

查看:890
本文介绍了JPA多对多关系导致无限递归和堆栈溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个EclipseLink项目,其中一个用户可以跟随另一个用户,就像在社交媒体网站上一样。我使用 User 实体(引用名为 users 的表)进行设置,该实体具有关注者列表(关注该用户的用户)和关注的另一个列表(该用户关注的用户)。该关系在称为追随者的单独表中定义,该表包含用于跟随的用户ID( user_id )的列以及以下内容用户ID( follower_id )。

I'm working on an EclipseLink project in which one user can "follow" another as can be done on social media sites. I have this set up with a User entity (referencing a table called users) which has a list of "followers" (users who follow that user) and another list of "following" (users that user is following). The relationship is defined in a separate table called followers which contains columns for the followed user's ID (user_id) and the following user's 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:

The getFollowers() method seems to work fine, but when getFollowing() is called I get a bunch of console spam that culminates in a 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)
...

请让我知道是否应该提供更多的堆栈跟踪信息。

Please let me know if I should provide more of the stack trace. Any hints?

推荐答案

每次您有 @OneToMany (一个集合) ),您需要向其中添加 @JsonIgnore ,否则将导致无限循环,从而导致堆栈溢出异常,因为它会在父级(一侧)之间不断查找和孩子(多方面)
有关处理此类问题的更多信息,请参见这篇出色的文章 http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

Every time you have @OneToMany (a collection) you need to add @JsonIgnore to it or else it will cause an infinite loop which results in a stack overflow exception because it keeps looking up between the parent(the one side) and the child (the many side) For more info on dealing with this kind of problems check this excellent article http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

这篇关于JPA多对多关系导致无限递归和堆栈溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆