即使数据库中只插入了一行,hibernate也会自动加载多个重复的行

时间:2017-03-18 09:03:16

标签: java spring hibernate

我有两个实体。 我目前面临的问题是,即使我的数据库中只插入了一行数据,我也会重复使用json值。

我的Questions.java实体看起来像这样。

Questions.java

@Entity
@Table
public class Questions {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private  int 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)
private List<Answers> answers = 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 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;
}
}

这是另一个实体的answer.java

Answers.java

@Entity
public class Answers {


@Id
@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.EAGER)
@JoinColumn(name = "question_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;
}
}

我的控制器保存数据(测试)以及返回json。

SaveApiContoller.java

@Controller    公共类SaveApiController {

@Autowired
SaveApiServices saveApiServices;

@Autowired
SaveQuestions saveQuestions;

@RequestMapping(value = "/saveData", method = RequestMethod.POST)
public String saveData(@RequestBody Answers answerss, Questions questionss){

    Answers answers = new Answers();
    answers.setAns_label("मुली");
    answers.setAns_value(1);
    answers.setAns_weightage(0);
    answers.setIs_default(0);

    Questions questions = new Questions();
    questions.setQuestion("१. व्यक्तिको पुरा नाम थर ?");
    questions.setType("input_edittext");
    questions.setDescription("");
    questions.setParam("smalltext");
    questions.setMaxlength(20);
    questions.setDependency("");

    answers.setQuestions(questions);
    saveApiServices.saveAnswers(answers);
    return null;
}


@RequestMapping("/getData")
public @ResponseBody List<Questions> getData(){
    List<Questions> questionss=saveApiServices.getQuestions();
    return questionss;
}
}

我需要像这样返回json。

[
    {
  "section": "खण्ड ग",
  "prefix": "c",
  "description": "व्यक्तिगत विवरण",
  "is_multiple": "1",
  "check_weight": "1",
  "questions": [

   {
      "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"
        }
      ]
    },
    {
      "id": "33",
      "question": "५. परिवारमूलीसँगको नाता",
      "type": "input_spinner",
      "description": null,
      "param": "",
      "maxlength": "1",
      "dependency": null,
      "answers": [  
        {
          "ans_label": "नातिनी",
          "ans_value": "15",
          "ans_weightage": "0",
          "is_default": "0"
        },
        {
          "ans_label": "धर्म पुत्र वा सौतेनी छोरा",
          "ans_value": "16",
          "ans_weightage": "0",
          "is_default": "0"
        }
      ]
    }
    ]

不幸的是,目前我得到一个很长的json列表:

  [{"id":1,"question":"१. व्यक्तिको पुरा नाम थर?", "type":"input_edittext",   
  "description":"","param":"smalltext","maxlength":20,"dependency":"",
  "answers": 
  [{"id":0,"ans_label":"मुली","ans_value":1,"ans_weightage":0,"is_default":0,
  "questions":{"id":1,"question":"१. व्यक्तिको पुरा नाम थर?",
 "type":"input_edittext","description":"","param":"smalltext","maxlength":20  
 ,"dependency":"","answers":[{"id":0,"ans_label":"मुली","ans_value":1,
 "ans_weightage":0,"is_default":0,"questions":{"id":1,
 "question":"१. व्यक्तिको पुरा नाम थर ?","type":"input_edittext",
 "description":"","param":"smalltext","maxlength":20,"dependency":"",
 "answers":[{"id":0,"ans_label":"मुली",
 "ans_value":1,"ans_weightage":0,"is_default":0,"questions":{"id":1,
 "question":"१. व्यक्तिको पुरा नाम थर ?","type":"input_edittext",
 "description":"","param":"smalltext","maxlength":20,"dependency":"",

  -----------------and more like this repeated----------------------

我想知道为什么这些相同的值会重复,即使我的数据库表只包含一行。

我想知道cascade是否是CascadeType.All是问题家伙,但如果我从answers.java中删除casecade类型,我将无法从控制器保存数据。

1 个答案:

答案 0 :(得分:0)

您不使用方法saveData(@RequestBody ...)的参数,而是 new 一个答案和问题对象,它只是问题对象的记录。