Spring Boot JPA一对多同时更新多个实体

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

Spring Boot JPA update multiple entities at once one to many

问题

以下是翻译好的内容:

我需要更新所有按ID筛选的实体,我可以单独为单个实体执行此操作,而且效果很好,但是如何对多个实体执行此操作呢?

以下是单个一对一更新的成功示例:

  1. @Override
  2. public FullPack updatePack(FullPack fullPack, int id) {
  3. FullPack newFullPack = fullPackRepository.findByPackId(id);
  4. newFullPack.setPackCompose(fullPack.getPackCompose());
  5. newFullPack.setPackSequence(fullPack.getPackPackSequence());
  6. FullPack savedFullPack = fullPackRepository.save(newFullPack);
  7. return savedFullPack;
  8. }

假设我会根据findByPackId获得多个FullPack,那么如何更新所有实体呢?

  1. @Override
  2. public List<FullPack> updatePack(FullPack fullPack, int id) {
  3. List<FullPack> newFullPackList = fullPackRepository.findByPackId(id);
  4. //////////////////
  5. /////////////////
  6. }

如何处理这种情况的代码呢?

英文:

I need to update all entities which filter by ID and I could do this for single entity and it works fine but how can I do this for multiple entities.

Here is successful example for single one to one update

  1. @Override
  2. public FullPack updatePack(FullPack fullPack, int id) {
  3. FullPack newFullPack = fullPackRepository.findByPackId(id);
  4. newFullPack.setPackCompose(fullPack.getPackCompose());
  5. newFullPack.setPackSequence(fullPack.getPackPackSequence());
  6. FullPack savedFullPack = fullPackRepository.save(newFullPack);
  7. return savedFullPack;
  8. }

Assume that I will get multiple FullPack based on findByPackId then how can I update all

  1. @Override
  2. public List&lt;FullPack&gt; updatePack(FullPack fullPack, int id) {
  3. List&lt;FullPack&gt; newFullPack = fullPackRepository.findByPackId(id);
  4. //////////////////
  5. /////////////////
  6. }

How can I do this kind of scenario

答案1

得分: 2

使用for循环迭代列表:

  1. @Override
  2. public List<FullPack> updatePack(FullPack fullPack, int id) {
  3. List<FullPack> newFullPackList = fullPackRepository.findByPackId(id);
  4. for (FullPack newFullPack : newFullPackList) {
  5. newFullPack.setPackCompose(fullPack.getPackCompose());
  6. newFullPack.setPackSequence(fullPack.getPackPackSequence());
  7. }
  8. fullPackRepository.saveAll(newFullPackList);
  9. return newFullPackList;
  10. }
英文:

Iterate the list with a for loop:

  1. @Override
  2. public List&lt;FullPack&gt; updatePack(FullPack fullPack, int id) {
  3. List&lt;FullPack&gt; newFullPackList = fullPackRepository.findByPackId(id);
  4. for (FullPack newFullPack : newFullPackList) {
  5. newFullPack.setPackCompose(fullPack.getPackCompose());
  6. newFullPack.setPackSequence(fullPack.getPackPackSequence());
  7. }
  8. fullPackRepository.saveAll(newFullPackList);
  9. return newFullPackList;
  10. }

答案2

得分: 1

使用JPA,大多数(如果不是全部)Repository接口都会暴露出.saveAll()方法,为您提供了保存集合的途径。

您可以像这样简单地保存多个FullPack。

  1. @Override
  2. public List<FullPack> updatePack(FullPack fullPack, int id) {
  3. List<FullPack> newFullPack = fullPackRepository.findByPackId(id);
  4. ...
  5. fullPackRepository.saveAll(newFullPack);
  6. }
英文:

With JPA, most, in not all Repository interfaces exposes a .saveAll() method that opens a path for you to save a collection.

You can simply save multiple FullPack like so.

  1. @Override
  2. public List&lt;FullPack&gt; updatePack(FullPack fullPack, int id) {
  3. List&lt;FullPack&gt; newFullPack = fullPackRepository.findByPackId(id);
  4. ...
  5. fullPackRepository.saveAll(newFullPack)
  6. }

答案3

得分: 0

你可以使用一个Repository来实现这个功能:

  1. @Query("update OrdineMacchina o set o.durataEsecuzione = o.durataEsecuzione + :incremento where o.idOrdineMacchina = :idOrdineMacchina")
  2. @Modifying
  3. void updateDuration(@Param("idOrdineMacchina") Long idOrdineMacchina, @Param("incremento") Integer incremento);

不要忘记添加 @Modifying 注解。在需要进行更新的地方,记得使用 @Autowired 来注入这个Repository。你可以将查询编写为JPQL语句或者本地查询。

英文:

you can do it with a Repository

  1. @Query(&quot;update OrdineMacchina o set o.durataEsecuzione = o.durataEsecuzione + :incremento where o.idOrdineMacchina = :idOrdineMacchina&quot;)
  2. @Modifying
  3. void updateDuration(@Param(&quot;idOrdineMacchina&quot;) Long idOrdineMacchina,@Param(&quot;incremento&quot;) Integer incremento);

don't foreget @Modifing. You have to @autowire the repository where you need the update.
You can write the query in JPQL or as a native query.

huangapple
  • 本文由 发表于 2020年9月3日 03:01:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63712023.html
匿名

发表评论

匿名网友

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

确定