如何使用cascade.all双向

时间:2014-03-03 15:51:58

标签: java hibernate

我想要做的是当我保存School对象时,我还想在数据库中保存(或更新)学生。如果学生得到更新,那么城市也必须更新。

class Student{

  @JoinColumn(....)
  @ManyToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER)
  private School school;

  @JoinColumn(....)
  @ManyToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER)
  private City city;

}


class School{

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "....", fetch=fetchType.LAZY)
  private List<Student> studentList;

}

1 个答案:

答案 0 :(得分:0)

class Student{

  @ManyToOne
  private School school;

  @ManyToOne(cascade = CascadeType.MERGE)
  private City city;

}


class School{

  @OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE})
  private List<Student> studentList;

}