jpa 多对多 StackOverflowError

错误内容:

Caused by: java.lang.StackOverflowError
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:737)
    ... 1011 more
//用户的所有优惠券集合
    @ManyToMany(mappedBy = "users", fetch = FetchType.LAZY)
    private List<Coupon> coupons = new ArrayList<>();
@JoinTable(
            name = "hakj_user_user_coupon",
            joinColumns = {@JoinColumn(name = "c_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "u_id", referencedColumnName = "id")}
    )
    @ManyToMany(fetch = FetchType.LAZY)
    private List<User> users = new ArrayList<>();

 

原因:两个toString都会互相查,所以造成死循环

解决:改掉一个toString或两个都改掉,将toString中,改掉对对方的显示,例如上面的,toString中没有coupons

posted @ 2018-04-27 16:37  猴子1  阅读(335)  评论(0编辑  收藏  举报