英文:
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<FullPack> updatePack(FullPack fullPack, int id) {
List<FullPack> 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<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;
}
答案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<FullPack> updatePack(FullPack fullPack, int id) {
List<FullPack> 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("update OrdineMacchina o set o.durataEsecuzione = o.durataEsecuzione + :incremento where o.idOrdineMacchina = :idOrdineMacchina")
@Modifying
void updateDuration(@Param("idOrdineMacchina") Long idOrdineMacchina,@Param("incremento") 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论