I have a problem with creating objects in spring-boot application. I have 3 classes, that are dependent from each other. I have 3 classes.
public class Examine { private Integer examineId; private String title; @JsonManagedReference private Set<Question> questions; public Examine(Integer examineId, String title) { this.examineId = examineId; this.title = title; this.questions = new HashSet<>(); } } public class Question { private Integer questionId; private String description; @JsonBackReference private Examine examine; @JsonManagedReference private Set<Answer> answers; public Question(Integer questionId, String description, Examine examine) { this.questionId = questionId; this.description = description; this.examine = examine; this.answers = new HashSet<>(); } } public class Answer { private Integer answerId; private String answer; @JsonBackReference private Question question; public Answer(Integer answerId, String answer) { this.answerId = answerId; this.answer = answer; } } What my problem is... I am not really sure in which order I should create objects. My Service classes are listed below. I start creating objects from class, that is most outside - Examine.
- For Examine class I have pretty easy method
createExamine()
public void createExamine(Examine examine) { examineRepository.save(examine); } - For Question class I have an idea to pass the
examineIdand then assign question to the specific examine.
public void createQuestion(Question question, Integer examineId) { Examine examine = examineService.getExamine(examineId); question.setExamine(examine); questionRepository.save(question); } - For Answer class I have the same idea as for Question - pass the
questionIdand assign answer to the specific question.
public void createAnswer(Answer answer, Integer questionId) { Question question = questionService.getQuestion(questionId); answer.setQuestion(question); answerRepository.save(answer); } Could you help me / tell me if I think in the right way? As for Examine class I'm pretty sure that it's okay - I have doubt about another two classes - if the way of creating them and linking to each other is correct.