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.
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?
– Victor Stafusa
By the way, if I may pun, if you have a problem with Questions and Answers, the result is Stackoverflow. :)
– Victor Stafusa
kkkkk good, the output format should be JSON.
– Felipe Paetzold