JPA @ManyToMany 报错StackOverflowError

在使用 SpringBoot + JPA 的@ManyToMany 遇到了如下报错

java.lang.StackOverflowError: null

2021-02-07 10:59:59.490 ERROR 100440 --- [io-20012-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is org.springframework.http.converter.HttpMessageNotWritableException: 
Could not write JSON: Infinite recursion (StackOverflowError); 
nested exception is com.fasterxml.jackson.databind.JsonMappingException: 
Infinite recursion (StackOverflowError) (through reference chain: 
com.xxx.entity.boem.EquipmentManage["dataPublishes"]->org.hibernate.collection.internal.PersistentSet[0]
->com.xxx.entity.bods.DataPublish["equipmentManages"]->org.hibernate.collection.internal.PersistentBag[0]
->com.xxx.entity.boem.EquipmentManage["dataPublishes"]->org.hibernate.collection.internal.PersistentSet[0]
->com.xxx.entity.bods.DataPublish["equipmentManages"]->org.hibernate.collection.internal.PersistentBag[0]
->com.xxx.entity.boem.EquipmentManage["dataPublishes"]->org.hibernate.collection.internal.PersistentSet[0]
->.......
..........

注意:
使用@ManyToMany时, 对应的Entity不可使用lombok 的@Data 注解。使用@Setter 、@Getter注解。主要原因是要自己覆写hash() equals(),toString() 方法。这样添加和删除的时候不会出现异常。否则出现循环的引用,不能删除或stackOver;
不能删除和添加成功,出现循环的主要问题在 toString()方法。此方法只能包含基本的元素,不要包含相应的@ManyToMany 的对象 。两个类都是。这样才会ok.

@Setter
@Getter
@Entity
public class User {


    @Id
    @GenericGenerator(name="jpauuid",strategy = "org.hibernate.id.UUIDGenerator")
    @GeneratedValue(generator = "jpauuid")
    @Column(length = 32,nullable = false)
    private String  id;

    @Column(length = 30)
    private String username;

    @ManyToMany(cascade = CascadeType.REFRESH,mappedBy = "users")
    private Set<Role> roles;


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return id.equals(user.id) &&
                username.equals(user.username) &&
                roles.equals(user.roles);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, username, roles);
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", roles=" + roles +
                '}';
    }
}
@Setter
@Getter
@Entity
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(length = 30)
    private String  name;


    @ManyToMany(cascade = CascadeType.REFRESH)
    @JoinTable(name = "user_role",joinColumns = @JoinColumn(name = "role_id"),inverseJoinColumns = @JoinColumn(name="user_id"))
    private Set<User>  users;

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值