外键列正在插入null

时间:2017-03-18 17:52:16

标签: java spring hibernate

我一直在挠头一个多小时,无法找到解决方案。我有两个实体类。其中一个是Questions.java,另一个是Answers.java

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

}

我从邮递员发送的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"
        }
      ]
   }

问题和答案数据全部存储在答案表中的外键列之外。专栏&#34; ques_id&#34;被存储为NULL。

1 个答案:

答案 0 :(得分:-1)

您必须记住的一件事是,为了使持久性提供程序能够级联保存操作,您必须在关系的两端设置依赖关系。

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

不确定这是否是您问题的答案,但请尝试一下。