我有一个Spring Restful API,它使用H2数据库和JPA填充一些表。我正在尝试使用字段@OneToMany
设置一个实体,而另一个实体对象是@ManyToOne
。在邮递员中发出POST请求时,我sout
Quiz quiz
是请求正文,并且字段是预期的。但是,当我登录到localhost上的数据库时,只有quiz_table
被更新了正确的属性。
我执行POST请求时会在控制台中获得此输出
Hibernate: insert into quiz_table (answer, text, title, id) values (?, ?, ?, ?)
我的JSON请求正文是
{
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": ["2002", "2019"],
"answer": 1
}
服务器响应为
{
"id": 67,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 0,
"quiz": null,
"option": "2002"
},
{
"id": 0,
"quiz": null,
"option": "2019"
}
]
}
据我所知"options"
的值已传递给List<Option>
这是我的控制器,具有该端点的映射
@PostMapping(path = "api/quizzes", produces = "application/json")
public Quiz addQuiz(@Valid @RequestBody Quiz quiz) {
quizRepository.save(quiz);
return quiz;
}
Quiz
类
@Component
@Entity(name = "quiz_table")
public class Quiz {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotBlank(message = "Title must not be blank")
private String title;
@NotBlank(message = "Text must not be blank")
private String text;
@Size(min = 2)
// @NotNull
@OneToMany(mappedBy = "quiz")
private List<Option> options;
@JsonIgnore
private int answer;
public Quiz() {}
public Quiz(String title, String text, List<Option> options, int answer) {
this.title = title;
this.text = text;
this.options = options;
this.answer = answer;
}
Option
类
@Component
@Entity(name = "options_table")
public class Option {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
@JoinColumn
private Quiz quiz;
private String option;
// for Jackson deserielization
public Option() {
}
/*public Option(Quiz quiz, String option) {
this.quiz = quiz;
this.option = option;
}*/
public Option(String option) {
this.option = option;
}
// getters/setters
编辑:实施建议后出现异常的JSON响应
{
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win a trophy?",
"options": [
{
"id": 2,
"quiz": {
"id": 1,
"title": "Tottenham Quiz",
"text": "When did Tottenham Hotspur last win
// ... continues for 4000 lines!
答案 0 :(得分:3)
@PostMapping(path = "api/quizzes", produces = "application/json")
public Quiz addQuiz(@Valid @RequestBody Quiz quiz) {
quiz.getOptions().forEach(option -> option.setQuiz(quiz));
quizRepository.save(quiz);
return quiz;
}
Quiz
,也要级联Option
@Size(min = 2)
@OneToMany(mappedBy = "quiz", cascade = CascadeType.ALL)
private List<Option> options;