liveBook · Manning

@Modifying annotation

As explained in Spring Start Here, Second Edition

The @Modifying annotation marks a Spring Data repository query method as performing a data-changing operation rather than only retrieving data.

@Modifying marks a Spring Data repository query method as changing data rather than retrieving it. Use it with @Query when the query performs an INSERT, UPDATE, or DELETE. section 14.3 section 14.5

Listing 14.7 Defining a modifying operation in the repository
public interface AccountRepository 
  extends CrudRepository<Account, Long> {
 
  @Query("SELECT * FROM account WHERE name = :name")
  List<Account> findAccountsByName(String name);
 
  @Modifying    #A
  @Query("UPDATE account SET amount = :amount WHERE id = :id")
  void changeAmount(long id, BigDecimal amount);
 
}

The repository method changes an account balance with an UPDATE query; @Modifying marks the operation as data-changing. section 14.3

How Spring Start Here, Second Edition covers it

How deep it goes
Defines its use for INSERT, UPDATE, and DELETE queries, and demonstrates it with an account-update example.
Examples
Updates an account’s amount by ID through an annotated repository method.
What you can do after
Apply it in Spring Data by marking a repository method whose query executes an INSERT, UPDATE, or DELETE.
How it connects
Pairs with @Query on Spring Data repository methods for data-changing operations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.