Java spring: Infinite recursion (Stackoverflowerror) error

Asked

Viewed 661 times

4

I’m having trouble carrying out the method list of translations of the document, I’m not understanding the reason for the error, follows code:

Translation.java
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Translation {
    @Id
    @GeneratedValue
    private Long id;

    private String title;

    private String description;


    //muitas traduções para um documento
    @ManyToOne()
    @JoinColumn(name="id_document", referencedColumnName="id", nullable=false)
    private Document document;
}

Document.java
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Document {
    @Id
    @GeneratedValue
    private Long id;

    private String title;

    private String description;

    //um documento com muitas traduções
    @OneToMany(mappedBy="document")
    private Set<Translation> translations;
}

The mistake:

Exceptionhandlerexceptionresolver : Resolved Exception caused by Handler Execution: 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: model. Translation["Document"]->model.Document["Translations"]->org.hibernate.Collection.internal.Persistentset[0]->model.Translation["Document"]->cmodel.Document["Translations"]....

Thank you

  • 1

    I really found the problem in this link, thank you.

1 answer

5


Problem solved by adding annotations @JsonManagedReference in Document and @JsonBackReference in Translation, example below:

    Document.java
     ...
    //um documento com muitas traduções
    @OneToMany(mappedBy="document")
    @JsonManagedReference
    private Set<Translation> translations;


    Translation.java
     ...
    //muitas traduções para um documento
    @ManyToOne()
    @JsonBackReference
    private Document document;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.