我刚开始学习spring-boot-jpa。我尝试在我的程序中使用一对一映射,当我使用 Getmapping 获取输出时,我得到了多次重复的相同数据。我不确定代码有什么问题。
一对一的亲子关系
@OneToOne(mappedBy = "question", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Options options;
一对一儿童关系
@OneToOne (cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "questionId")
private Question question;
下面是控制器类
@RestController
public class QuizController {
List<Question> question = new ArrayList<Question>();
@Autowired
QuestionRepo questionRepo;
@Autowired
OptionsRepo optionsRepo;
@GetMapping("/question")
public List<Question> getQues() {
return questionRepo.findAll();
}
@PostMapping("/question")
public void setQues(@RequestParam String ques, String choice1, String choice2, String choice3, String choice4) {
Question setQues = new Question();
setQues.setQuestion(ques);
Options setOp = new Options();
setOp.setOption1(choice1);
setOp.setOption2(choice2);
setOp.setOption3(choice3);
setOp.setOption4(choice4);
setQues.setOptions(setOp);
setOp.setQuestion(setQues);
questionRepo.save(setQues);
这是我得到的输出,相同的选项重复多次,
[{"questionId":1,"options":{"option1":"Red","option2":"Black","option3":"Yellow","option4":"Green","question":{"questionId":1,"options":{"option1":"Red","option2":"Black","option3":"Yellow","option4":"Green"}]