更新HashSet <object> values </object>

时间:2015-02-19 07:04:53

标签: java spring hibernate relational-database hashset

我的目标是更新Hashset值。我已经映射了两个表格,其中包括onetomany&amp;很多人的关系。

我的流程 我有考试和考试表。在考试中我创建了具有onetomany的Set examPapers = new HashSet().I ExamPaper我刚刚创建了与manytoone的关系。我已成功将值存储在两个表中,我在更新部分中出错。请检查我的以下编码。

POJO考试

private String examName;
private Set<ExamPaper> examPapers = new HashSet<ExamPaper>();

@Column(name = "exam_name") 
@Field
public String getExamName() {
    return examName;
}
public void setExamName(String examName) {
    this.examName = examName;
}

@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER)
@JoinColumn(name="examination_id")
public Set<ExamPaper> getExamPapers() {
    return examPapers;
}
public void setExamPapers(Set<ExamPaper> examPapers) {
    this.examPapers = examPapers;
}

POJO ExamPaper

private String maxMark;
private Examination examination;

@Column(name = "maxmark") 
@Field
public String getMaxMark() {
    return maxMark;
}
public void setMaxMark(String maxMark) {
    this.maxMark = maxMark;
}

@ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER)
@JoinColumn(name="examination_id")
public Examination getExamination() {
    return examination;
}
public void setExamination(Examination examination) {
    this.examination = examination;
}

我的控制器

Set<ExamPaper> examPapers=new HashSet<ExamPaper>();
String[] maxMarks=request.getParameterValues("maxMarks");
for (int i=0;i<maxMarks.length;i++) {
   ExamPaper examPaper = new ExamPaper();
   examPaper.setMaxMark(maxMarks[i]);
   examPapers.add(examPaper);
}
examination.setExamPapers(examPapers);
examinationService.saveOrUpdateExamination(examination);

此代码已成功保存到DB中。我正在尝试更新(即添加maxMarks或删除maxMarks)。它将ExamPaper表值加倍并从ExamPaper中删除了考试ID。我已经附加了DB的保存和更新图片。

注:第一部分考试,每部分的第二部分考试页。

保存 - http://postimg.org/image/xzg8qu3dh/

更新 - http://postimg.org/image/t22o5q1ed/

如何更新?

1 个答案:

答案 0 :(得分:0)

我认为,您的映射中存在错误,请尝试使用此选项:

POJO考试

 private Set<ExamPaper> examPapers = new HashSet<ExamPaper>();

 @OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER,  mappedBy='examination')
 public Set<ExamPaper> getExamPapers() {
  return examPapers;
 }

POJO ExamPaper

 private Examination examination;

 @ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER)
 @JoinColumn(name="examination_id")
 public Examination getExamination() {
    return examination;
 }

之后,如果您仍然遇到同样的问题,请在控制器内的for循环中添加以下行:

 examPaper.setExamination(examination);
 examPapers.add(examPaper);