我有两个名为Qa.java和Answeres.java的实体类
我的Qa实体包含答案列表。
Qa.Java
@Entity
@Table(name = "qa")
public class Qa {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
private String question;
private String type;
private String description;
private String param;
private int maxlength;
@OneToMany(mappedBy = "qa", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Answers> answersList = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
public int getMaxlength() {
return maxlength;
}
public void setMaxlength(int maxlength) {
this.maxlength = maxlength;
}
public List<Answers> getAnswersList() {
return answersList;
}
public void setAnswersList(List<Answers> answersList) {
this.answersList = answersList;
}
}
Answers.java
@Entity
@Table(name = "answers")
public class Answers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String ans_label;
private int ans_value;
private int ans_weightage;
private int is_default;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "question_id", referencedColumnName = "id",nullable = false)
private Qa qa;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAns_label() {
return ans_label;
}
public void setAns_label(String ans_label) {
this.ans_label = ans_label;
}
public int getAns_value() {
return ans_value;
}
public void setAns_value(int ans_value) {
this.ans_value = ans_value;
}
public int getAns_weightage() {
return ans_weightage;
}
public void setAns_weightage(int ans_weightage) {
this.ans_weightage = ans_weightage;
}
public int getIs_default() {
return is_default;
}
public void setIs_default(int is_default) {
this.is_default = is_default;
}
public Qa getQa() {
return qa;
}
public void setQa(Qa qa) {
this.qa = qa;
}
}
我想要插入数据的控制器。
TableDataController.java
@Controller
public class TabletDataController {
@Autowired
QaRepository qaRepository;
@RequestMapping(value = "/saveApiData", method = RequestMethod.GET)
public void saveApiData(){
Qa qa = new Qa();
qa.setParam("");
qa.setType("input_spinner");
qa.setDescription("");
qa.setQuestion("व्यक्तिको पहिलो नाम ?");
ArrayList<Answers> answersArrayList = new ArrayList<>();
Answers answers = new Answers();
answers.setAns_label("नेपाली");
answers.setAns_value(1);
answers.setAns_weightage(0);
answers.setIs_default(0);
Answers answers1 = new Answers();
answers1.setAns_label("english");
answers1.setAns_value(2);
answers1.setAns_weightage(2);
answers1.setIs_default(2);
Answers answers2 = new Answers();
answers2.setAns_label("japense");
answers2.setAns_value(3);
answers2.setAns_weightage(3);
answers2.setIs_default(3);
answersArrayList.add(answers);
answersArrayList.add(answers1);
answersArrayList.add(answers2);
qa.setAnswersList(answersArrayList);
answers.setQa(qa);
qaRepository.save(qa);
}
}
我的qaRepository扩展了JpaRepository。所以每当我称这个api只是第一个对象,即
Answers answers = new Answers();
answers.setAns_label("नेपाली");
answers.setAns_value(1);
answers.setAns_weightage(0);
answers.setIs_default(0);
已保存。我怎样才能保存所有给定的清单?
答案 0 :(得分:1)
您还必须将qa对象设置为answers1和answers2对象,而不仅仅是answer对象。