diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java index d441938f0..77d718244 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java @@ -54,6 +54,7 @@ import org.springframework.util.ReflectionUtils; * @author Thomas Darimont * @author Mark Paluch * @author Nicolas Cirigliano + * @author Jens Schauder */ public abstract class JpaQueryExecution { @@ -221,13 +222,14 @@ public abstract class JpaQueryExecution { /** * Creates an execution that automatically flushes the given {@link EntityManager} before execution and/or - * clears the given {@link EntityManager} after execution if the given {@link EntityManager} is not - * {@literal null}. + * clears the given {@link EntityManager} after execution. * - * @param em + * @param em Must not be {@literal null}. */ public ModifyingExecution(JpaQueryMethod method, EntityManager em) { + Assert.notNull(em, "The EntityManager must not be null."); + Class returnType = method.getReturnType(); boolean isVoid = void.class.equals(returnType) || Void.class.equals(returnType); @@ -243,13 +245,13 @@ public abstract class JpaQueryExecution { @Override protected Object doExecute(AbstractJpaQuery query, Object[] values) { - if (em != null && flush) { + if (flush) { em.flush(); } int result = query.createQuery(values).executeUpdate(); - if (em != null && clear) { + if (clear) { em.clear(); } diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java index b1915c38a..6f2408f3a 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java @@ -287,7 +287,7 @@ public class JpaQueryMethod extends QueryMethod { /** * Returns whether we should flush automatically for modifying queries. * - * @return + * @return whether we should flush automatically. */ boolean getFlushAutomatically() { return getMergedOrDefaultAnnotationValue("flushAutomatically", Modifying.class, Boolean.class); @@ -296,7 +296,7 @@ public class JpaQueryMethod extends QueryMethod { /** * Returns whether we should clear automatically for modifying queries. * - * @return + * @return whether we should clear automatically. */ boolean getClearAutomatically() { return getMergedOrDefaultAnnotationValue("clearAutomatically", Modifying.class, Boolean.class); diff --git a/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java index 683602f03..958fb9353 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java @@ -28,6 +28,7 @@ import javax.persistence.EntityManager; import javax.persistence.Query; import javax.persistence.TypedQuery; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -47,6 +48,7 @@ import org.springframework.data.repository.query.Parameters; * @author Thomas Darimont * @author Mark Paluch * @author Nicolas Cirigliano + * @author Jens Schauder */ @RunWith(MockitoJUnitRunner.class) public class JpaQueryExecutionUnitTests { @@ -58,6 +60,14 @@ public class JpaQueryExecutionUnitTests { @Mock TypedQuery countQuery; + @Before + public void setUp(){ + + when(query.executeUpdate()).thenReturn(0); + when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query); + when(jpaQuery.getQueryMethod()).thenReturn(method); + } + @Test(expected = IllegalArgumentException.class) public void rejectsNullQuery() { @@ -83,15 +93,12 @@ public class JpaQueryExecutionUnitTests { }.execute(jpaQuery, new Object[] {}), is(nullValue())); } - @Test + @Test // DATAJPA-806 @SuppressWarnings({ "unchecked", "rawtypes" }) public void modifyingExecutionFlushesEntityManagerIfSet() { - when(query.executeUpdate()).thenReturn(0); when(method.getReturnType()).thenReturn((Class) void.class); when(method.getFlushAutomatically()).thenReturn(true); - when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query); - when(jpaQuery.getQueryMethod()).thenReturn(method); ModifyingExecution execution = new ModifyingExecution(method, em); execution.execute(jpaQuery, new Object[] {}); @@ -104,11 +111,8 @@ public class JpaQueryExecutionUnitTests { @SuppressWarnings({ "unchecked", "rawtypes" }) public void modifyingExecutionClearsEntityManagerIfSet() { - when(query.executeUpdate()).thenReturn(0); when(method.getReturnType()).thenReturn((Class) void.class); when(method.getClearAutomatically()).thenReturn(true); - when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query); - when(jpaQuery.getQueryMethod()).thenReturn(method); ModifyingExecution execution = new ModifyingExecution(method, em); execution.execute(jpaQuery, new Object[] {});