Spring JPA 双向无法评估 toString [英] Spring JPA bi-directional cannot evaluate toString

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

问题描述

我已经通过 @JsonIdentityInfo 解决了 JSON 递归循环到 Baeldung 的博客

请问有什么问题,为什么?

解决方案

好吧,我的猜测是 Registration.toString() 打印列表中每个付款的字符串表示,并且由于 Payment.toString() 包含Registration 的字符串表示,Registration.toString() 被再次调用,进而调用Payment.toString() 再次,依此类推.

尝试在 Payment.toString() 中返回一个空字符串以查看问题是否消失.

I have resolved JSON recursive loop with @JsonIdentityInfothrough to Baeldung's blog1 (Thanks)

But now, another error occurs :

Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate com.mezoo.tdc.model.Payment.toString()

Here my Registration object :

    @Entity
    @Table(name="Registration")
    @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
    public class Registration implements Serializable {

       /*some private variables..*/

      // Bidirectional relationship
      @OneToMany(mappedBy="registration", cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, fetch = FetchType.LAZY)
      private List<Payment> payment;

                @Override
      public String toString() {
         return MoreObjects.toStringHelper(this)
            .add("payment", payment)
            .toString();
         }
    }

Now, Payment object :

    @Entity
    @Table(name="Payment")
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
    public class Payment implements Serializable {
       @ManyToOne
       @JoinColumn(name = "registration")
       private Registration registration;

       @Override
       public String toString() {
       return MoreObjects.toStringHelper(this)
            .add("registration", registration)
            .toString();
       }
    }

This is, what I see in debugger :

Please, what is wrong and why ?

解决方案

Well, my guess is that Registration.toString() prints the string representation of each payment in the list, and since Payment.toString() includes the string representation of Registration, Registration.toString() is called again, which in turn calls Payment.toString() again, and so on.

Try to return an empty string in Payment.toString() to see if the problem goes away.

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

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