节能战略在春季启动应用程序时,我们有儿童(一对多和cascade.all)列表的父实体,我们使用dto保存策略是什么?弹簧数据的JPA我们使用Hibernate实现</p> <p>DTO转换到实体JPA
我们必须循环子,与dto比较,如果存在元素,更新值,如果它们是新的,添加元素,如果删除了某些元素,将它从列表中删除?
例
Parent parent = repo.findById(Integer id);
//remove items who dont exit anymore
Childs childs = bean.getChild();
for (Iterator<Childs> iterator = childs.iterator(); iterator.hasNext();) {
//compare with dto...
}
for (ChildsDto childsDto : ChildsDto) {
if(childsDto==null){
//add new element in the list of childs of parent
}else{
update element in the list of childs of parent
}
}
作者:“只要childsDto包含id,你可以使他们成为Childs列表,然后将列表设置为父级,通过保存父级,列表将根据需要合并。”你的意思是采取每一个ID,做一个查询数据库,采取豆,更新字段... –
我更新了答案,并希望它更清楚我的意思 – Tom