Stackoverflow in bidirectional relationship at JPA

Asked

Viewed 1,008 times

4

I’m facing this problem with a bi-directional relationship, when I put in a question to create an answer it makes persistence, even then beauty, but if I try to get a get or even in the answer the API points me an http 500, I found that the problem is: Question contains a list of Answer, Answer contains a Question, which contains a list of Answer, which contains a Question .... tending to infinity, someone knows how to stop it?


@Entity
public class Question extends BasicForum {

    private String title;
    private Integer views;

    @OneToMany(fetch= FetchType.LAZY, cascade= CascadeType.ALL, mappedBy = "question")
    private List<Answer> answerList;

    public void transformAnswerInList(Answer answer){
        List<Answer> answers = getAnswerList();
        answers.add(answer);
        this.setAnswerList(answers);
    }
}

@Entity
public class Answer extends BasicForum {

    @ManyToOne
    private Question question;

}

@RestController
@RequestMapping(QuestionController.MAPPING)
public class QuestionController extends SuperController<Question, QuestionRepository> {
    public static final String MAPPING = "/api/questions";

    @Autowired
    private QuestionRepository repository;

    @Override
    public QuestionRepository getRepository() {
        return repository;
    }

    //Outros metodos ocultos

    @RequestMapping(method = RequestMethod.PUT, value = "/{id}/answers")
    public ResponseEntity<Question> postAnswer(@PathVariable("id") final String id, @RequestBody Answer answer){
        Question question = repository.findOne(id);
        question.addAnswer(answer);
        answer.setQuestion(question);
        return super.update(question);
    }
}

I’m using Generics and liquibase, I don’t know if it can be that. Thanks already.

  • 1

    Your problem seems to be with serialization. That is, it is time to convert a Question or Answer to a String format. In your case, what format should the output be in? JSON? XML? HTML? What is the component, class or configuration that makes this conversion?

  • By the way, if I may pun, if you have a problem with Questions and Answers, the result is Stackoverflow. :)

  • kkkkk good, the output format should be JSON.

1 answer

3


this problem happens, when Spring tries to serialize the object to JSON, and as there are circular references, it plays an Exception indicating that, for you to solve this problem, you usually have 2 options, these being:

1 - You can annotate a field to be ignored in the serialization with @Jsonignore, in this case it would be in the list of Answer or Question.

2 - You can create a custom Jackson serializer and you format the serialization to suit you.

Note: You can still try using 2 Annotations, but it is not certain to solve this problem.

@Entity
public class Answer extends BasicForum {

    @ManyToOne
    @JsonBackReference
    private Question question;
}

@Entity
public class Question extends BasicForum {

    @OneToMany(fetch= FetchType.LAZY, cascade= CascadeType.ALL, mappedBy = "question")
    @JsonManagedReference
    private List<Answer> answerList;
}
  • In this case the problem is that json ignores the references of the other entity, and I need to present this information to the user on the front :/

  • Okay, in case there’s no escape, you’ll have to create custom serializer of Jackson, have you put the 2 Annotations I told you about? Look at this link, it teaches how to create custom serializer, http://www.baeldung.com/jackson-custom-serialization.

  • I used, only on the side of Answer, in this case solved in parts, because Question hence gets the array of Answer normally, but in Answer I lose the reference of Question in Json, I will try to use a Serializer, thanks for the force.

  • Don’t forget to play favorites aahhahaha :p

  • Haha, it’s already done

Browser other questions tagged

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