Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

hibernate - Spring JPA bi-directional cannot evaluate 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 :

debug screenshot

Please, what is wrong and why ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Remove toString method.

In case you are using Lombok:

Check you Entity/DAO class you might be using @Data annotation from lombok which by default includes getter and setters. Change it to @Getters and @Setter in case you need those and remove @Data annotation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...