我使用树结构和使用@ManyToOne和@OneToMany批注的父子关系创建了一个实体。但是,只有我对实体的父级所做的更改才会在数据库中处理。
我的实体类如下:
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class OKR {
@Id
@Column(name = "ID")
@GeneratedValue
private UUID id;
@NotBlank
private String name;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "parentid", referencedColumnName = "id")
private OKR parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private List<OKR> children;
private boolean isRoot;
public OKR(String name, OKR parent, List<OKR> children){
this.name = name;
this.children = children;
if (this.children==null){
this.children = new ArrayList<>();
}
this.parent = parent;
if (parent == null||parent.equals(new UUID(0,0))){
isRoot = true;
}else{
isRoot = false;
}
}
protected OKR(){
isRoot = true;
children = new ArrayList<>();
}
当我通过更改其父对象来更新OKR时,父OKR也将被更新。但是,当我仅通过添加子项来更新OKR时,子项OKR不会得到更新。我是JPA的新手,我还注意到数据库中没有用于儿童的表。因此,我的问题是,仅更新实体的子代时更新所有关系的最简单方法是什么?