Update multiple fields with JPA query

To update multiple fields in DB with JPA you have to use @Modifying and @Query. Here, an example:

@Repository
public interface CarRepository extends JpaRepository<CarDto, String> {

     //Here we update fields color and brand dynamically using parameters
     @Modifying
     @Query("update CarDto c set c.color = :color, c.brand = :brand where c.numberOfDoors = 5")
     void updateColorAndBrand(@Param("color") String color, @Param("brand") String brand);

}

For more interesting tutorials & guides just check them HERE.

Leave a Reply

Your email address will not be published. Required fields are marked *