What is the use of @Modifying annotation?

@Modifying is used alongside @Query on repository methods to mark a query as a write operation -- an UPDATE or DELETE -- rather than a SELECT, telling Spring Data JPA to execute it differently and propagate its changes to the database.

Key Points

Example

Bulk-deactivating all users who haven't logged in for a year is more efficient as a single UPDATE statement than loading every matching entity into memory and saving each one, so the repository method is annotated with both @Query and @Modifying.

public interface UserRepository extends JpaRepository<User, Long> {

    @Modifying
    @Transactional
    @Query("UPDATE User u SET u.active = false WHERE u.lastLogin < :cutoff")
    int deactivateInactiveUsers(@Param("cutoff") LocalDate cutoff);
}

Interview Tip

A concise interview answer is

@Modifying marks a @Query method as a write operation, an UPDATE or DELETE, so Spring Data JPA executes it as a bulk statement instead of expecting a result set. I pair it with @Transactional, and often clearAutomatically = true, to avoid the persistence context holding stale entity state after the bulk change.

Last updated: 16 July 2026

Previous question: What is the purpose of save() method in CrudRepository?

Next question: What is the difference between findById() and getOne()?

More Spring Data JPA interview questions

All Spring Data JPA questions