通过父级更新删除添加子实体列表
Update delete add list of child entities through parent
是否有更好的方法add, update, delete
子实体(LIST)基于新传递的子实体列表?
我更新的函数
Parent parent = findById(id);
//make changes to parent attributes
//parent.setXyz(dto.getXyz());
//convert newly passed sons to hashmap
Map<String, Son> newSons = toHashMap(dto.getSons());
List<Son> finalSons = new ArrayList<>();
for (Son oldSon : parent.getSons()) {
if (newSons.containsKey(oldSon.getUniqueString())) {
finalSons.add(oldSon);
newSons.remove(oldSon.getUniqueString());
} else {
//Delete the son thats not in new list
sonRepository.delete(oldSon);
}
}
//add remaining sons
for (Son son : newSons) {
finalSons.add(son);
}
//existing.getSons().clear();
//existing.getSons().addAll(finalSons);
existing.getSons().clear();
parentRepository.save(parent);
父实体
@Entity(name = "parents")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Son> sons;
}
子实体
@Entity(name = "sons")
public class Son {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "parentId")
private Parent parent;
}
当您使用 JPA 的 orphanRemoval
功能时,您的更新过程可能会更轻松一些,但要小心,因为它可能会导致您不自觉地删除内容:
Parent.java
@Entity(name = "parents")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private List<Son> sons;
}
服务
Parent parent = findById(id);
Map<String, Son> addedSons = toHashMap(dto.getSons());
// Remove existing sons that are not part of the update
parent.getSons().removeIf(existingSon -> !addedSons.containsKey(existingSon.getUniqueString()));
// Add new sons
addedSons.forEach(parent.getSons()::add);
// Save parent and trigger creating of added sons/deletion of removed existing sons
parentRepository.save(parent);
是否有更好的方法add, update, delete
子实体(LIST)基于新传递的子实体列表?
我更新的函数
Parent parent = findById(id);
//make changes to parent attributes
//parent.setXyz(dto.getXyz());
//convert newly passed sons to hashmap
Map<String, Son> newSons = toHashMap(dto.getSons());
List<Son> finalSons = new ArrayList<>();
for (Son oldSon : parent.getSons()) {
if (newSons.containsKey(oldSon.getUniqueString())) {
finalSons.add(oldSon);
newSons.remove(oldSon.getUniqueString());
} else {
//Delete the son thats not in new list
sonRepository.delete(oldSon);
}
}
//add remaining sons
for (Son son : newSons) {
finalSons.add(son);
}
//existing.getSons().clear();
//existing.getSons().addAll(finalSons);
existing.getSons().clear();
parentRepository.save(parent);
父实体
@Entity(name = "parents")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Son> sons;
}
子实体
@Entity(name = "sons")
public class Son {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "parentId")
private Parent parent;
}
当您使用 JPA 的 orphanRemoval
功能时,您的更新过程可能会更轻松一些,但要小心,因为它可能会导致您不自觉地删除内容:
Parent.java
@Entity(name = "parents")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private List<Son> sons;
}
服务
Parent parent = findById(id);
Map<String, Son> addedSons = toHashMap(dto.getSons());
// Remove existing sons that are not part of the update
parent.getSons().removeIf(existingSon -> !addedSons.containsKey(existingSon.getUniqueString()));
// Add new sons
addedSons.forEach(parent.getSons()::add);
// Save parent and trigger creating of added sons/deletion of removed existing sons
parentRepository.save(parent);