Skip to content

Commit 7fd992d

Browse files
committed
DATAJPA-1485 - Clarified documentation for @Modifying.
1 parent adb8eb1 commit 7fd992d

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/main/asciidoc/jpa.adoc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,9 @@ In the preceding example, the `MappedTypeRepository` interface is the common par
464464
[[jpa.modifying-queries]]
465465
=== Modifying Queries
466466

467-
All the previous sections describe how to declare queries to access a given entity or collection of entities. You can add custom modifying behavior by using the facilities described in "`<<repositories.custom-implementations>>`". As this approach is feasible for comprehensive custom functionality, you can modify queries that only need parameter binding by annotating the query method with `@Modifying`, as shown in the following example:
467+
All the previous sections describe how to declare queries to access a given entity or collection of entities.
468+
You can add custom modifying behavior by using the facilities described in "`<<repositories.custom-implementations>>`".
469+
As this approach is feasible for comprehensive custom functionality, you can modify queries that only need parameter binding by annotating the query method with `@Modifying`, as shown in the following example:
468470

469471
.Declaring manipulating queries
470472
====
@@ -476,7 +478,11 @@ int setFixedFirstnameFor(String firstname, String lastname);
476478
----
477479
====
478480

479-
Doing so triggers the query annotated to the method as an updating query instead of a selecting one. As the `EntityManager` might contain outdated entities after the execution of the modifying query, we do not automatically clear it (see the https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html[JavaDoc] of `EntityManager.clear()` for details), since this effectively drops all non-flushed changes still pending in the `EntityManager`. If you wish the `EntityManager` to be cleared automatically, you can set the `@Modifying` annotation's `clearAutomatically` attribute to `true`.
481+
Doing so triggers the query annotated to the method as an updating query instead of a selecting one. As the `EntityManager` might contain outdated entities after the execution of the modifying query, we do not automatically clear it (see the https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html[JavaDoc] of `EntityManager.clear()` for details), since this effectively drops all non-flushed changes still pending in the `EntityManager`.
482+
If you wish the `EntityManager` to be cleared automatically, you can set the `@Modifying` annotation's `clearAutomatically` attribute to `true`.
483+
484+
The `@Modifying` annotation is only relevant in combination with the `@Query` annotation.
485+
Derived query methods or custom methods do not require this Annotation.
480486

481487
[[jpa.modifying-queries.derived-delete]]
482488
==== Derived Delete Queries

src/main/java/org/springframework/data/jpa/repository/Modifying.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@
2222
import java.lang.annotation.Target;
2323

2424
/**
25+
* <p>
2526
* Indicates a query method should be considered as modifying query as that changes the way it needs to be executed.
26-
* This annotation is only considered if used on actual query methods (either derived or manually defined through a
27-
* {@link Query} annotation). It's not applied on custom implementation methods as they already have control over the
28-
* underlying data access APIs.
27+
* This annotation is only considered if used on query methods defined through a {@link Query} annotation). It's not
28+
* applied on custom implementation methods or queries derived from the method name as they already have control over
29+
* the underlying data access APIs or specify if they are modifying by their name.
30+
* </p>
31+
* <p>
32+
* Queries that require a `@Modifying` annotation include {@code INSERT}, {@code UPDATE}, {@code DELETE}, and DDL
33+
* statements.
34+
* </p>
2935
*
3036
* @author Oliver Gierke
3137
* @author Christoph Strobl
3238
* @author Nicolas Cirigliano
39+
* @author Jens Schauder
3340
* @see Query
3441
*/
3542
@Retention(RetentionPolicy.RUNTIME)

src/main/java/org/springframework/data/jpa/repository/Query.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* @author Oliver Gierke
3030
* @author Thomas Darimont
3131
* @author Christoph Strobl
32+
*
33+
* @see Modifying
3234
*/
3335
@Retention(RetentionPolicy.RUNTIME)
3436
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })

0 commit comments

Comments
 (0)