Skip to content

Commit 81d9f8a

Browse files
committed
Polishing.
Simplify code flow. Introduce flag to capture whether a stored procedure uses collection return types. Remove unconditionally the Optional converter as we're already on Java 8 and do not require the Java 8 guard. See #2915 Original pull request: #2938
1 parent 44465bf commit 81d9f8a

File tree

3 files changed

+18
-43
lines changed

3 files changed

+18
-43
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public AbstractJpaQuery(JpaQueryMethod method, EntityManager em) {
9292
if (method.isStreamQuery()) {
9393
return new StreamExecution();
9494
} else if (method.isProcedureQuery()) {
95-
return new ProcedureExecution();
95+
return new ProcedureExecution(method.isCollectionQuery());
9696
} else if (method.isCollectionQuery()) {
9797
return new CollectionExecution();
9898
} else if (method.isSliceQuery()) {

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

+17-41
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public abstract class JpaQueryExecution {
6666

6767
conversionService.addConverter(JpaResultConverters.BlobToByteArrayConverter.INSTANCE);
6868
conversionService.removeConvertible(Collection.class, Object.class);
69-
potentiallyRemoveOptionalConverter(conversionService);
69+
conversionService.removeConvertible(Object.class, Optional.class);
7070

7171
CONVERSION_SERVICE = conversionService;
7272
}
@@ -172,7 +172,7 @@ static class PagedExecution extends JpaQueryExecution {
172172

173173
@Override
174174
@SuppressWarnings("unchecked")
175-
protected Object doExecute(final AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor accessor) {
175+
protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor accessor) {
176176

177177
Query query = repositoryQuery.createQuery(accessor);
178178

@@ -303,45 +303,41 @@ protected Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccesso
303303
*/
304304
static class ProcedureExecution extends JpaQueryExecution {
305305

306+
private final boolean collectionQuery;
307+
306308
private static final String NO_SURROUNDING_TRANSACTION = "You're trying to execute a @Procedure method without a surrounding transaction that keeps the connection open so that the ResultSet can actually be consumed. Make sure the consumer code uses @Transactional or any other way of declaring a (read-only) transaction.";
307309

308-
/*
309-
* (non-Javadoc)
310-
* @see org.springframework.data.jpa.repository.query.JpaQueryExecution#doExecute(org.springframework.data.jpa.repository.query.AbstractJpaQuery, java.lang.Object[])
311-
*/
310+
ProcedureExecution(boolean collectionQuery) {
311+
this.collectionQuery = collectionQuery;
312+
}
313+
312314
@Override
313315
protected Object doExecute(AbstractJpaQuery jpaQuery, JpaParametersParameterAccessor accessor) {
314316

315317
Assert.isInstanceOf(StoredProcedureJpaQuery.class, jpaQuery);
316318

317-
StoredProcedureJpaQuery storedProcedureJpaQuery = (StoredProcedureJpaQuery) jpaQuery;
318-
319-
StoredProcedureQuery storedProcedure = storedProcedureJpaQuery.createQuery(accessor);
319+
StoredProcedureJpaQuery query = (StoredProcedureJpaQuery) jpaQuery;
320+
StoredProcedureQuery procedure = query.createQuery(accessor);
320321

321322
try {
322323

323-
boolean returnsResultSet = storedProcedure.execute();
324+
boolean returnsResultSet = procedure.execute();
324325

325326
if (returnsResultSet) {
326327

327328
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
328329
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
329330
}
330331

331-
if (storedProcedureJpaQuery.getQueryMethod().isCollectionQuery()) {
332-
return storedProcedure.getResultList();
333-
} else {
334-
return storedProcedure.getSingleResult();
335-
}
332+
return collectionQuery ? procedure.getResultList() : procedure.getSingleResult();
336333
}
337334

338-
return storedProcedureJpaQuery.extractOutputValue(storedProcedure);
339-
335+
return query.extractOutputValue(procedure);
340336
} finally {
341337

342-
if (storedProcedure instanceof AutoCloseable autoCloseable) {
338+
if (procedure instanceof AutoCloseable) {
343339
try {
344-
autoCloseable.close();
340+
((AutoCloseable) procedure).close();
345341
} catch (Exception ignored) {}
346342
}
347343
}
@@ -358,14 +354,14 @@ static class StreamExecution extends JpaQueryExecution {
358354

359355
private static final String NO_SURROUNDING_TRANSACTION = "You're trying to execute a streaming query method without a surrounding transaction that keeps the connection open so that the Stream can actually be consumed. Make sure the code consuming the stream uses @Transactional or any other way of declaring a (read-only) transaction.";
360356

361-
private static Method streamMethod = ReflectionUtils.findMethod(Query.class, "getResultStream");
357+
private static final Method streamMethod = ReflectionUtils.findMethod(Query.class, "getResultStream");
362358

363359
/*
364360
* (non-Javadoc)
365361
* @see org.springframework.data.jpa.repository.query.JpaQueryExecution#doExecute(org.springframework.data.jpa.repository.query.AbstractJpaQuery, JpaParametersParameterAccessor)
366362
*/
367363
@Override
368-
protected Object doExecute(final AbstractJpaQuery query, JpaParametersParameterAccessor accessor) {
364+
protected Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccessor accessor) {
369365

370366
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
371367
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
@@ -386,24 +382,4 @@ protected Object doExecute(final AbstractJpaQuery query, JpaParametersParameterA
386382
}
387383
}
388384

389-
/**
390-
* Removes the converter being able to convert any object into an {@link Optional} from the given
391-
* {@link ConversionService} in case we're running on Java 8.
392-
*
393-
* @param conversionService must not be {@literal null}.
394-
*/
395-
public static void potentiallyRemoveOptionalConverter(ConfigurableConversionService conversionService) {
396-
397-
ClassLoader classLoader = JpaQueryExecution.class.getClassLoader();
398-
399-
if (ClassUtils.isPresent("java.util.Optional", classLoader)) {
400-
401-
try {
402-
403-
Class<?> optionalType = ClassUtils.forName("java.util.Optional", classLoader);
404-
conversionService.removeConvertible(Object.class, optionalType);
405-
406-
} catch (ClassNotFoundException | LinkageError o_O) {}
407-
}
408-
}
409385
}

src/test/java/org/springframework/data/jpa/repository/support/JpaRepositoryTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class JpaRepositoryTests {
5959

6060
@BeforeEach
6161
void setUp() {
62-
6362
repository = new JpaRepositoryFactory(em).getRepository(SampleEntityRepository.class);
6463
idClassRepository = new JpaRepositoryFactory(em).getRepository(SampleWithIdClassRepository.class);
6564
}

0 commit comments

Comments
 (0)