MySQLIntegrityConstraintViolationException:column" question_id"不能为null错误

时间:2017-03-26 12:09:08

标签: java mysql spring hibernate

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

    answersArrayList.add(answers);

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

}
}

我的qaRepository扩展了JpaRepository。因此每当我调用此API时,我都会收到com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Column&#39; question_id&#39;不能为空

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您有双向OneToMany关系,因此您需要手动维护关系的两个方面。在这里,您只需使用Qa

设置qa.setAnswersList(answersArrayList);

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

answers.setQa(qa);
保存列表前

答案 1 :(得分:0)

代码如下

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.setQa(qa);
    answersArrayList.add(answers);
    qa.setAnswersList(answersArrayList);
    qaRepository.save(qa);
}

保存时。您应该级联保存。您的注释会配置关联表的关系,但也会在保存时关联它们