Annotation Type Modifying


@Retention(CLASS) @Target(METHOD) public @interface Modifying

Marks a repository @Query method whose statement is a bulk UPDATE or DELETE.

The statement runs in the active transaction and answers the number of rows it changed. It goes straight to the database: per-entity callbacks and cascades do not run, and entities this session already manages are not changed by it. An entity read before the statement keeps the values it had then, while the row may now hold others. It stays managed, so a change made to it afterwards is still written when the transaction commits. Read the new values with Session.refresh(entity), or set clearAutomatically so the session is emptied after the statement and the next read goes to the database.

A row that a bulk DELETE removed can still be held by the session as an entity. Changing that entity, or removing it, fails at flush with an OptimisticLockException, because its row is gone.

  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Whether to detach every entity this session manages once the statement has run, so that what is read afterwards is read from the database.
    boolean
    Whether to flush the session's pending changes before the statement.
  • Element Details

    • flushAutomatically

      boolean flushAutomatically

      Whether to flush the session's pending changes before the statement.

      Pending changes are flushed before every bulk statement whatever this says, so the statement sees what the transaction already changed. That is what a JPA provider's default AUTO flush mode does before a statement that reads tables with pending writes; here every pending change is written, as the session does not narrow a flush to the tables a statement reads. Skipping the flush could only make the statement miss changes the transaction made, so the attribute is accepted for source compatibility with Spring Data and has no further effect.

      Returns:
      ignored; the session is always flushed first
      Default:
      false
    • clearAutomatically

      boolean clearAutomatically

      Whether to detach every entity this session manages once the statement has run, so that what is read afterwards is read from the database.

      Off by default, as in Spring Data. Turning it on detaches entities the caller may still be holding: a change made to one of them after this method returns is not tracked and is never written. Make such changes first, or read the entity again after the call.

      Returns:
      true to clear the session after the statement
      Default:
      false