Spring Hibernate双向ManyToMany StackOverflowError [英] Spring Hibernate Bidirectional ManyToMany StackOverflowError

查看:278
本文介绍了Spring Hibernate双向ManyToMany StackOverflowError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立双向ManyToMany关联.

因此,我有一个名为User的实体:

import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @ManyToMany(mappedBy="users")
    private List<Chat> chats = new ArrayList<>();
}

另一个叫聊天的人:

import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class Chat {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @ManyToMany
    @JoinTable(name = "chat_user",
        joinColumns = { @JoinColumn(name = "fk_chat") },
        inverseJoinColumns = { @JoinColumn(name = "fk_user") })
    private List<User> users = new ArrayList<>();
}

因此,当我尝试执行以下操作时:

Chat chat = new Chat();
User user = new User();

user.getChats().add(chat);
chat.getUsers().add(user); // Getting an exception!!!

得到这个:

Method trew 'java.lang.StackOverflowExceptionError' exception.
Cannot evaluate hello.models.Chat.toString()

我认为问题是:聊天具有用户,该用户引用了该聊天,该用户具有再次引用该聊天的用户,依此类推.

那我该如何解决呢?

解决方案

是的,您永无止境的递归是正确的.

我们如何解决这个问题?可以尝试这些步骤

1.如果您在提到的实体中使用了toString(),请从中删除其他实体的引用.

2.可以在关系的一侧添加@JsonIgnore,这会破坏链条

    @ManyToMany(mappedBy="users")
    @JsonIgnore
    private List<Chat> chats = new ArrayList<>();

请参阅本文以了解更多方式

3.我注意到您正在使用Lombok,在这种情况下,请排除toString批注的属性,并且可以编写自定义的toString并牢记第1点.

I'm trying to build bidirectional ManyToMany association.

So, I have an entity called User:

import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @ManyToMany(mappedBy="users")
    private List<Chat> chats = new ArrayList<>();
}

And another one called Chat:

import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
public class Chat {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @ManyToMany
    @JoinTable(name = "chat_user",
        joinColumns = { @JoinColumn(name = "fk_chat") },
        inverseJoinColumns = { @JoinColumn(name = "fk_user") })
    private List<User> users = new ArrayList<>();
}

So, when I'm trying to do:

Chat chat = new Chat();
User user = new User();

user.getChats().add(chat);
chat.getUsers().add(user); // Getting an exception!!!

Getting this one:

Method trew 'java.lang.StackOverflowExceptionError' exception.
Cannot evaluate hello.models.Chat.toString()

I think the problem is: Chat has user, that refences to that Chat that has user that references to that Chat again and so on.

So how can I solve it?

解决方案

Yes, you are right with never-ending recursion.

How do we solve this problem? Can try these steps

1 . If you have used toString() in mentioned entities, remove reference of other entity from it.

2 . Can add @JsonIgnore at one side of relation which will break the chain

    @ManyToMany(mappedBy="users")
    @JsonIgnore
    private List<Chat> chats = new ArrayList<>();

Refer to this article for more ways

3 . I notice you are using Lombok, in that case, exclude attribute of the toString annotation and may be can write custom toString keeping point 1 in mind.

这篇关于Spring Hibernate双向ManyToMany StackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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