Skip to content

Commit b1ae132

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

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
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

+3-2
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.
@@ -49,6 +49,7 @@
4949
* @author Oliver Gierke
5050
* @author Thomas Darimont
5151
* @author Mark Paluch
52+
* @author Nicolas Cirigliano
5253
*/
5354
public abstract class AbstractJpaQuery implements RepositoryQuery {
5455

@@ -134,7 +135,7 @@ protected JpaQueryExecution getExecution() {
134135
} else if (method.isPageQuery()) {
135136
return new PagedExecution(method.getParameters());
136137
} else if (method.isModifyingQuery()) {
137-
return method.getClearAutomatically() ? new ModifyingExecution(method, em) : new ModifyingExecution(method, null);
138+
return new ModifyingExecution(method, em);
138139
} else {
139140
return new SingleEntityExecution();
140141
}

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

+14-4
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.
@@ -53,6 +53,7 @@
5353
* @author Oliver Gierke
5454
* @author Thomas Darimont
5555
* @author Mark Paluch
56+
* @author Nicolas Cirigliano
5657
*/
5758
public abstract class JpaQueryExecution {
5859

@@ -215,10 +216,13 @@ protected Object doExecute(AbstractJpaQuery query, Object[] values) {
215216
static class ModifyingExecution extends JpaQueryExecution {
216217

217218
private final EntityManager em;
219+
private final boolean flush;
220+
private final boolean clear;
218221

219222
/**
220-
* Creates an execution that automatically clears the given {@link EntityManager} after execution if the given
221-
* {@link EntityManager} is not {@literal null}.
223+
* Creates an execution that automatically flushes the given {@link EntityManager} before execution and/or
224+
* clears the given {@link EntityManager} after execution if the given {@link EntityManager} is not
225+
* {@literal null}.
222226
*
223227
* @param em
224228
*/
@@ -232,14 +236,20 @@ public ModifyingExecution(JpaQueryMethod method, EntityManager em) {
232236
Assert.isTrue(isInt || isVoid, "Modifying queries can only use void or int/Integer as return type!");
233237

234238
this.em = em;
239+
this.flush = method.getFlushAutomatically();
240+
this.clear = method.getClearAutomatically();
235241
}
236242

237243
@Override
238244
protected Object doExecute(AbstractJpaQuery query, Object[] values) {
239245

246+
if (em != null && flush) {
247+
em.flush();
248+
}
249+
240250
int result = query.createQuery(values).executeUpdate();
241251

242-
if (em != null) {
252+
if (em != null && clear) {
243253
em.clear();
244254
}
245255

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

+12-2
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.
@@ -49,6 +49,7 @@
4949
* @author Oliver Gierke
5050
* @author Thomas Darimont
5151
* @author Christoph Strobl
52+
* @author Nicolas Cirigliano
5253
*/
5354
public class JpaQueryMethod extends QueryMethod {
5455

@@ -284,10 +285,19 @@ String getNamedCountQueryName() {
284285
}
285286

286287
/**
287-
* Returns whether we should clear automatically for modifying queries.
288+
* Returns whether we should flush automatically for modifying queries.
288289
*
289290
* @return
290291
*/
292+
boolean getFlushAutomatically() {
293+
return getMergedOrDefaultAnnotationValue("flushAutomatically", Modifying.class, Boolean.class);
294+
}
295+
296+
/**
297+
* Returns whether we should clear automatically for modifying queries.
298+
*
299+
* @return
300+
*/
291301
boolean getClearAutomatically() {
292302
return getMergedOrDefaultAnnotationValue("clearAutomatically", Modifying.class, Boolean.class);
293303
}

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.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)