Spring数据:批量删除实体列表

huangapple go评论78阅读模式
英文:

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&lt;Integer&gt; 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&lt;Entitity&gt; entities)

答案1

得分: 3

但是 CrudRepository 恰好有这个方法:deleteAll

而且 JpaRepository 有:deleteInBatch

英文:

But CrudRepository has exactly that method: deleteAll

And JpaRepository has: 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&lt;? extends Identity&gt; entities);

void deleteInBatch(Iterable&lt;T&gt; entities);

void deleteAllInBatch();

Or create something like

@Query(&quot;Delete From Identity i Where i.id In :ids&quot;)
void deleteCustom(@Param(&quot;ids&quot;) Iterable&lt;? extends Long&gt; 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&lt;Entitity&gt; entities){
   foreach(Entitity entitie:entities){
     delete(entitie);
 }
}

huangapple
  • 本文由 发表于 2020年10月20日 23:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64448542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定