gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:14 27 4
gpt4 key购买 nike

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

My 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;
}
}

这是另一个实体answers.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;
}
}

我的 Controller 保存数据(测试)并返回 json。

SaveApiContoller.java

@ Controller 公共(public)类 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 类型,我将无法从 Controller 保存数据。

最佳答案

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

关于java - 即使数据库中只插入一行,hibernate 也会自动加载多个重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42872383/

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