Spring JPA双向无法评估toString [英] Spring JPA bi-directional cannot evaluate toString
问题描述
我已经通过@JsonIdentityInfo
解决了JSON递归循环,直到Baeldung的博客 1 (谢谢)
I have resolved JSON recursive loop with @JsonIdentityInfo
through to Baeldung's blog1 (Thanks)
但是现在,另一个错误发生了:
But now, another error occurs :
Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate com.mezoo.tdc.model.Payment.toString()
这是我的注册对象:
@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();
}
}
现在,付款对象:
@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 ?
推荐答案
好吧,我的猜测是Registration.toString()
打印列表中每笔付款的字符串表示形式,并且由于Payment.toString()
包括Registration
的字符串表示形式,再次调用Registration.toString()
,依次又调用Payment.toString()
,依此类推.
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.
尝试在Payment.toString()
中返回一个空字符串,以查看问题是否消失.
Try to return an empty string in Payment.toString()
to see if the problem goes away.
这篇关于Spring JPA双向无法评估toString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!