I thought I understood the JPA's @JoinColumns annotation and mappedBy parameter, but then I needed to create new entities from this Json of a Question. It has a set of answer choices which need to be mapped to new entities as well. I decided that the Question entity is gonna be the owning side, therefore I omitted the mappedBy parameter. When I used the @JoinColumns annotation on the AnswerChoice side, all the entities were created from the Json objects, but the AnswerChoices' FKs to the Question entity were not set.
Putting the @JoinColumns in the Question entity solved the problem, but my question is: is this the correct way? Will I be facing any side effects? Should I instead have run a for-loop on the set of AnswerChoices and set the FK?
Question Json
{
"text": "Do you know JPA?",
"answerChoices": [{
"text": "yes",
}, {
"text": "no",
}, ]
}
Controller with a JpaRepository:
@PostMapping("/questions/create")
@ResponseBody
public String create(@RequestBody Question json) {
questionRepo.save(json);
}
Question entity:
@Entity
public class Question {
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name="question_id")
private Set<AnswerChoice> answerChoices;
}
AnswerChoice entity:
@Entity
public class AnswerChoice {
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnore
private Question question;
}
For the sake of brevity I omitted the auto-generated Id's.
Nope, the mapping is not correct. It actually creates two separate associations that happen to share the join column.
Either remove
question
fromAnswer
, making the association unidirectional (ask yourself if you really need that side of the association) or go back to the original solution and use@JsonBackReference/@JsonManagedReference
(so that the field gets automatically populated during deserialization).