英文:
Spring data: bulk delete list of entities
问题
如何使用Spring JPA批量删除具有实体列表的数据?
以下方法可行:
void deleteByIdIn(List<Integer> ids)
然而,我有一个实体列表,而不是ID列表。我可以将实体列表转换为ID列表,但我宁愿不这样做。
我正在寻找类似这样的方法:void deleteAll(List<Entitity> entities)
英文:
How can I bulk delete with Spring JPA having list of entities?
The following would work:
void deleteByIdIn(List<Integer> ids)
However I do have a list of entities, not ids. I could convert the list to the list of ids, but I would prefer not to.
I am looking for something like: void deleteAll(List<Entitity> entities)
答案1
得分: 3
但是 CrudRepository
恰好有这个方法:deleteAll
而且 JpaRepository
有:deleteInBatch
答案2
得分: 1
根据您使用的仓库(Repository
和所有继承类),您可以调用
void deleteById(Long id);
void delete(Identity entity);
void deleteAll(Iterable<? extends Identity> entities);
void deleteInBatch(Iterable<T> entities);
void deleteAllInBatch();
或者创建类似于以下的内容
@Query("Delete From Identity i Where i.id In :ids")
void deleteCustom(@Param("ids") Iterable<? extends Long> ids);
英文:
Depending of the repo (Repository
and all inheritors) you use you can call
void deleteById(Long id);
void delete(Identity entity);
void deleteAll(Iterable<? extends Identity> entities);
void deleteInBatch(Iterable<T> entities);
void deleteAllInBatch();
Or create something like
@Query("Delete From Identity i Where i.id In :ids")
void deleteCustom(@Param("ids") Iterable<? extends Long> ids);
答案3
得分: -2
需要覆盖此方法:void deleteAll(List<Entity> entities)
void delete(Entity entity) {
repo.delete(entity);
}
void deleteAll(List<Entity> entities) {
for (Entity entity : entities) {
delete(entity);
}
}
英文:
u need override this Methode: void deleteAll(List<Entitity> entities)
void delete(Entitity entities){
repo.delete(entities);
}
void deleteAll(List<Entitity> entities){
foreach(Entitity entitie:entities){
delete(entitie);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论