0

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.

  1. For Examine class I have pretty easy method createExamine()
 public void createExamine(Examine examine) { examineRepository.save(examine); } 
  1. For Question class I have an idea to pass the examineId and 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); } 
  1. For Answer class I have the same idea as for Question - pass the questionId and 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.

1 Answer 1

1

In JPA the Ids are generated by the ORM layer.

You have to define an entity like this:

@Entity @Table(name = "EXAMINE") public class Examine implements Serializable { @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.AUTO) private long id; 

Then follow the mapping rules to anotate your other entities.

It is not important in what order you create the objects, to link them you need at least the two you are linking. so examine.setQuestion(question).

Same thing in the other objects.

in the end you save the top Object of your Domain Hierarchy. entityManager.save(examine);

Take a look at sth like this:

https://www.baeldung.com/spring-data-jpa-generate-db-schema

Sign up to request clarification or add additional context in comments.

5 Comments

So, the way that I link classes is alright? I'm talking about these two, where I pass two arguments (Object object, Integer otherObjectId)
The id does not have to be passed as parameter. The jpa layer generates it in connection with the dB.
But I'm talking about linking objects... I'm not sure if I explain it correctly to You. What I want to achieve is to link the question with the answer. Could you tell me how to do it in createAnswer() method?
If you need double linking then yes, pass the parent to the child and vice versa. But lieve the ids be handled from the framework.
Yes, I know that ids can be handled 'automatically' by framework. I was wondering about linking... child - parent relation. Thank you very much for your ensure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.