diff --git a/src/main/asciidoc/jpa.adoc b/src/main/asciidoc/jpa.adoc index a09ad6830..aa4e0a8a6 100644 --- a/src/main/asciidoc/jpa.adoc +++ b/src/main/asciidoc/jpa.adoc @@ -464,7 +464,9 @@ In the preceding example, the `MappedTypeRepository` interface is the common par [[jpa.modifying-queries]] === Modifying Queries -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 "`<>`". 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: +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 "`<>`". +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: .Declaring manipulating queries ==== @@ -476,7 +478,11 @@ int setFixedFirstnameFor(String firstname, String lastname); ---- ==== -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`. +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`. + +The `@Modifying` annotation is only relevant in combination with the `@Query` annotation. +Derived query methods or custom methods do not require this Annotation. [[jpa.modifying-queries.derived-delete]] ==== Derived Delete Queries diff --git a/src/main/java/org/springframework/data/jpa/repository/Modifying.java b/src/main/java/org/springframework/data/jpa/repository/Modifying.java index 42628a441..5940d607a 100644 --- a/src/main/java/org/springframework/data/jpa/repository/Modifying.java +++ b/src/main/java/org/springframework/data/jpa/repository/Modifying.java @@ -22,14 +22,21 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** + *

* Indicates a query method should be considered as modifying query as that changes the way it needs to be executed. - * This annotation is only considered if used on actual query methods (either derived or manually defined through a - * {@link Query} annotation). It's not applied on custom implementation methods as they already have control over the - * underlying data access APIs. + * This annotation is only considered if used on query methods defined through a {@link Query} annotation). It's not + * applied on custom implementation methods or queries derived from the method name as they already have control over + * the underlying data access APIs or specify if they are modifying by their name. + *

+ *

+ * Queries that require a `@Modifying` annotation include {@code INSERT}, {@code UPDATE}, {@code DELETE}, and DDL + * statements. + *

* * @author Oliver Gierke * @author Christoph Strobl * @author Nicolas Cirigliano + * @author Jens Schauder * @see Query */ @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/org/springframework/data/jpa/repository/Query.java b/src/main/java/org/springframework/data/jpa/repository/Query.java index d8c1ff0fe..6f6b0fed9 100644 --- a/src/main/java/org/springframework/data/jpa/repository/Query.java +++ b/src/main/java/org/springframework/data/jpa/repository/Query.java @@ -29,6 +29,8 @@ import org.springframework.data.annotation.QueryAnnotation; * @author Oliver Gierke * @author Thomas Darimont * @author Christoph Strobl + * + * @see Modifying */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })