Skip to content

Commit 86a4934

Browse files
ciri-cuervoschauder
authored andcommitted
DATAJPA-806 - Add flushAutomatically attribute to @Modifying annotation.
Original pull request: #172.
1 parent 3c536e3 commit 86a4934

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@
2626
*
2727
* @author Oliver Gierke
2828
* @author Christoph Strobl
29+
* @author Nicolas Cirigliano
2930
*/
3031
@Retention(RetentionPolicy.RUNTIME)
3132
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
3233
@Documented
3334
public @interface Modifying {
3435

36+
/**
37+
* Defines whether we should flush the underlying persistence context before executing the modifying query.
38+
*
39+
* @return
40+
*/
41+
boolean flushAutomatically() default false;
42+
3543
/**
3644
* Defines whether we should clear the underlying persistence context after executing the modifying query.
3745
*

src/main/java/org/springframework/data/jpa/repository/query/AbstractJpaQuery.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* @author Thomas Darimont
5353
* @author Mark Paluch
5454
* @author Christoph Strobl
55+
* @author Nicolas Cirigliano
5556
* @author Jens Schauder
5657
* @author Сергей Цыпанов
5758
*/
@@ -145,7 +146,7 @@ protected JpaQueryExecution getExecution() {
145146
} else if (method.isPageQuery()) {
146147
return new PagedExecution(method.getParameters());
147148
} else if (method.isModifyingQuery()) {
148-
return method.getClearAutomatically() ? new ModifyingExecution(method, em) : new ModifyingExecution(method, null);
149+
return new ModifyingExecution(method, em);
149150
} else {
150151
return new SingleEntityExecution();
151152
}

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2017 the original author or authors.
2+
* Copyright 2008-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,7 @@
5454
* @author Thomas Darimont
5555
* @author Mark Paluch
5656
* @author Christoph Strobl
57+
* @author Nicolas Cirigliano
5758
*/
5859
public abstract class JpaQueryExecution {
5960

@@ -218,11 +219,14 @@ protected Object doExecute(AbstractJpaQuery query, Object[] values) {
218219
*/
219220
static class ModifyingExecution extends JpaQueryExecution {
220221

221-
private final @Nullable EntityManager em;
222+
private final EntityManager em;
223+
private final boolean flush;
224+
private final boolean clear;
222225

223226
/**
224-
* Creates an execution that automatically clears the given {@link EntityManager} after execution if the given
225-
* {@link EntityManager} is not {@literal null}.
227+
* Creates an execution that automatically flushes the given {@link EntityManager} before execution and/or
228+
* clears the given {@link EntityManager} after execution if the given {@link EntityManager} is not
229+
* {@literal null}.
226230
*
227231
* @param em
228232
*/
@@ -236,14 +240,20 @@ public ModifyingExecution(JpaQueryMethod method, @Nullable EntityManager em) {
236240
Assert.isTrue(isInt || isVoid, "Modifying queries can only use void or int/Integer as return type!");
237241

238242
this.em = em;
243+
this.flush = method.getFlushAutomatically();
244+
this.clear = method.getClearAutomatically();
239245
}
240246

241247
@Override
242248
protected Object doExecute(AbstractJpaQuery query, Object[] values) {
243249

250+
if (em != null && flush) {
251+
em.flush();
252+
}
253+
244254
int result = query.createQuery(values).executeUpdate();
245255

246-
if (em != null) {
256+
if (em != null && clear) {
247257
em.clear();
248258
}
249259

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @author Oliver Gierke
5252
* @author Thomas Darimont
5353
* @author Christoph Strobl
54+
* @author Nicolas Cirigliano
5455
* @author Mark Paluch
5556
* @author Сергей Цыпанов
5657
*/
@@ -312,6 +313,15 @@ String getNamedCountQueryName() {
312313
return StringUtils.hasText(annotatedName) ? annotatedName : getNamedQueryName() + ".count";
313314
}
314315

316+
/**
317+
* Returns whether we should flush automatically for modifying queries.
318+
*
319+
* @return
320+
*/
321+
boolean getFlushAutomatically() {
322+
return getMergedOrDefaultAnnotationValue("flushAutomatically", Modifying.class, Boolean.class);
323+
}
324+
315325
/**
316326
* Returns whether we should clear automatically for modifying queries.
317327
*

src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @author Oliver Gierke
4747
* @author Thomas Darimont
4848
* @author Mark Paluch
49+
* @author Nicolas Cirigliano
4950
*/
5051
@RunWith(MockitoJUnitRunner.Silent.class)
5152
public class JpaQueryExecutionUnitTests {
@@ -82,18 +83,37 @@ protected Object doExecute(AbstractJpaQuery query, Object[] values) {
8283
}.execute(jpaQuery, new Object[] {}), is(nullValue()));
8384
}
8485

86+
@Test
87+
@SuppressWarnings({ "unchecked", "rawtypes" })
88+
public void modifyingExecutionFlushesEntityManagerIfSet() {
89+
90+
when(query.executeUpdate()).thenReturn(0);
91+
when(method.getReturnType()).thenReturn((Class) void.class);
92+
when(method.getFlushAutomatically()).thenReturn(true);
93+
when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query);
94+
when(jpaQuery.getQueryMethod()).thenReturn(method);
95+
96+
ModifyingExecution execution = new ModifyingExecution(method, em);
97+
execution.execute(jpaQuery, new Object[] {});
98+
99+
verify(em, times(1)).flush();
100+
verify(em, times(0)).clear();
101+
}
102+
85103
@Test
86104
@SuppressWarnings({ "unchecked", "rawtypes" })
87105
public void modifyingExecutionClearsEntityManagerIfSet() {
88106

89107
when(query.executeUpdate()).thenReturn(0);
90108
when(method.getReturnType()).thenReturn((Class) void.class);
109+
when(method.getClearAutomatically()).thenReturn(true);
91110
when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query);
92111
when(jpaQuery.getQueryMethod()).thenReturn(method);
93112

94113
ModifyingExecution execution = new ModifyingExecution(method, em);
95114
execution.execute(jpaQuery, new Object[] {});
96115

116+
verify(em, times(0)).flush();
97117
verify(em, times(1)).clear();
98118
}
99119

0 commit comments

Comments
 (0)