gpt4 book ai didi

java - 外键列被插入为空

转载 作者:行者123 更新时间:2023-12-01 06:06:49 25 4
gpt4 key购买 nike

我已经挠头一个多小时了,找不到解决方案。我有两个实体类。其中之一是 Questions.java,另一个是 Answers.java

问题.java

@Entity
@Table
public class Questions {
@Id
@Column(name = "ques_id")
private int ques_id;

@Column(name = "question")
private String question;

@Column(name = "type")
private String type;

@Column(name = "description")
private String description;

@Column(name = "param")
private String param;

@Column(name = "maxlength")
private int maxlength;

@Column(name = "dependency")
private String dependency;

@OneToMany(mappedBy = "questions",targetEntity = Answers.class, cascade = CascadeType.ALL)
private List<Answers> answers = new ArrayList<>();

public int getQues_id() {
return ques_id;
}

public void setQues_id(int ques_id) {
this.ques_id = ques_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 String getDependency() {
return dependency;
}

public List<Answers> getAnswers() {
return answers;
}

public void setAnswers(List<Answers> answers) {
this.answers = answers;
}

public void setDependency(String dependency) {
this.dependency = dependency;
}
}

Answers.java

@Entity
public class Answers {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Column(name = "ans_label")
private String ans_label;

@Column(name = "ans_value")
private int ans_value;

@Column(name = "ans_weightage")
private int ans_weightage;

@Column(name = "is_default")
private int is_default;

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "ques_id")
private Questions questions;

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 Questions getQuestions() {
return questions;
}

public void setQuestions(Questions questions) {
this.questions = questions;
}
}

SaveApiController.java

 @RequestMapping(value = "/saveData", method = RequestMethod.POST)
public void saveData(@RequestBody Questions questionss){
//questions.setAnswers(questionss.getAnswers());

saveQuestions.save(questionss);

}

我从 postman 发送的 json 数据如下所示。

  {
"id": "31",
"question": "३. लिङ्ग ?",
"type": "input_spinner",
"description": null,
"param": "",
"maxlength": "0",
"dependency": null,
"answers": [
{
"ans_label": "पुरुष",
"ans_value": "1",
"ans_weightage": "0",
"is_default": "0"
},
{
"ans_label": "महिला",
"ans_value": "2",
"ans_weightage": "0",
"is_default": "0"
},
{
"ans_label": "अन्य",
"ans_value": "3",
"ans_weightage": "0",
"is_default": "0"
}
]
}

问题和答案数据都与答案表中的外键列分开存储。列“ques_id”被存储为 NULL。

最佳答案

您必须记住的一件事是,为了让持久性提供程序级联保存操作,您必须设置关系双方的依赖关系。

因此,问题需要有一个答案列表,并且每个答案都需要引用问题实体。只有这样级联才会发生。

不确定这是否能解决您的问题,但请尝试一下。

关于java - 外键列被插入为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42877840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com