gpt4 book ai didi

java - MySQLIntegrityConstraintViolationException : column "question_id" cannot be null error

转载 作者:行者123 更新时间:2023-11-29 19:11:46 39 4
gpt4 key购买 nike

我有两个名为 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;
}
}

我尝试插入数据的 Controller 。

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);

answersArrayList.add(answers);

qa.setAnswersList(answersArrayList);
qaRepository.save(qa);

}
}

我的 qaRepository 扩展了 JpaRepository。所以每当我调用这个 api 时,我都会收到 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'question_id' can not be null 的错误

我做错了什么?

最佳答案

您有一个双向 OneToMany 关系,因此您需要手动维护关系的双方。在这里,您仅使用 qa.setAnswersList(answersArrayList);

设置 Qa

您需要手动设置关系的另一方。添加:

answers.setQa(qa);

保存列表之前

关于java - MySQLIntegrityConstraintViolationException : column "question_id" cannot be null error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43028491/

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