简体   繁体   中英

JPA Update Multiple Records in Table for a ID

I have a requirement for a Input record with id1 from source, in target table I need to update value v1 in column c1 and in target for id1 there are multiple records. Using JPA I need to update all those records with value v1. Using JPA what is the best way to do this?

I used below findallbyid() then saveall() - it failed saying there are mutliple records in target but expected was one.

Based on the details provided findallbyid() then saveall()

here the method findallbyid() is actually expecting to find only one record in the table, where as there are multiple rows.

changing the to signature of the method should work as expected without expection. As it expect capitalised words in method signature

https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html

List<T> findAllById(Long id);

but recommend not to read all rows and then save again just to update a column or two, you could use something like below to achieve the same

@Modifying
@Transactional
@Query(value = "UPDATE table t SET t.column = :status WHERE t.id = :id")
int update(@Param("status") String status, @Param("id") Long id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM