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

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

Spring Boot JPA update multiple entities at once one to many

问题

以下是翻译好的内容:

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

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

@Override
public FullPack updatePack(FullPack fullPack, int id) {

    FullPack newFullPack = fullPackRepository.findByPackId(id);
    newFullPack.setPackCompose(fullPack.getPackCompose());
    newFullPack.setPackSequence(fullPack.getPackPackSequence());
    FullPack savedFullPack = fullPackRepository.save(newFullPack);

    return savedFullPack;
}

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

@Override
public List<FullPack> updatePack(FullPack fullPack, int id) {

    List<FullPack> newFullPackList = fullPackRepository.findByPackId(id);
    //////////////////
    /////////////////
}

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

英文:

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

@Override
public FullPack updatePack(FullPack fullPack, int id) {

    FullPack newFullPack = fullPackRepository.findByPackId(id);
    newFullPack.setPackCompose(fullPack.getPackCompose());
	newFullPack.setPackSequence(fullPack.getPackPackSequence());
    FullPack savedFullPack = fullPackRepository.save(newFullPack);


    return savedFullPack;
} 

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

@Override
public List&lt;FullPack&gt; updatePack(FullPack fullPack, int id) {

    List&lt;FullPack&gt; newFullPack = fullPackRepository.findByPackId(id);
    //////////////////
    /////////////////
} 

How can I do this kind of scenario

答案1

得分: 2

使用for循环迭代列表:

@Override
public List<FullPack> updatePack(FullPack fullPack, int id) {

    List<FullPack> newFullPackList = fullPackRepository.findByPackId(id);
   
    for (FullPack newFullPack : newFullPackList) {
        newFullPack.setPackCompose(fullPack.getPackCompose());
        newFullPack.setPackSequence(fullPack.getPackPackSequence());
    }

    fullPackRepository.saveAll(newFullPackList);
    
    return newFullPackList;

}
英文:

Iterate the list with a for loop:

@Override
public List&lt;FullPack&gt; updatePack(FullPack fullPack, int id) {

    List&lt;FullPack&gt; newFullPackList = fullPackRepository.findByPackId(id);
   
    for (FullPack newFullPack : newFullPackList) {
        newFullPack.setPackCompose(fullPack.getPackCompose());
        newFullPack.setPackSequence(fullPack.getPackPackSequence());
    }

    fullPackRepository.saveAll(newFullPackList);
    
    return newFullPackList;

}

答案2

得分: 1

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

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

@Override
public List<FullPack> updatePack(FullPack fullPack, int id) {

    List<FullPack> newFullPack = fullPackRepository.findByPackId(id);
    ...
    fullPackRepository.saveAll(newFullPack);
}
英文:

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.

@Override
public List&lt;FullPack&gt; updatePack(FullPack fullPack, int id) {

    List&lt;FullPack&gt; newFullPack = fullPackRepository.findByPackId(id);
    ...
    fullPackRepository.saveAll(newFullPack)
} 

答案3

得分: 0

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

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

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

英文:

you can do it with a Repository

@Query(&quot;update OrdineMacchina o set o.durataEsecuzione = o.durataEsecuzione + :incremento where o.idOrdineMacchina = :idOrdineMacchina&quot;)
@Modifying
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:

确定