DATAJPA-1485 - Clarified documentation for @Modifying.

This commit is contained in:
Jens Schauder
2018-12-19 13:41:06 +01:00
parent adb8eb1537
commit 7fd992dcdd
3 changed files with 20 additions and 5 deletions

View File

@@ -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 "`<<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:
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:
.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

View File

@@ -22,14 +22,21 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>
* 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.
* </p>
* <p>
* Queries that require a `@Modifying` annotation include {@code INSERT}, {@code UPDATE}, {@code DELETE}, and DDL
* statements.
* </p>
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Nicolas Cirigliano
* @author Jens Schauder
* @see Query
*/
@Retention(RetentionPolicy.RUNTIME)

View File

@@ -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 })