Skip to content

Commit db28730

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 17 and do not require the Java 8 guard. See #2915 Original pull request: #2938
1 parent c09c73b commit db28730

File tree

3 files changed

+18
-39
lines changed

3 files changed

+18
-39
lines changed

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

Lines changed: 1 addition & 1 deletion
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()) {

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

Lines changed: 17 additions & 37 deletions
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
}
@@ -167,7 +167,7 @@ static class PagedExecution extends JpaQueryExecution {
167167

168168
@Override
169169
@SuppressWarnings("unchecked")
170-
protected Object doExecute(final AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor accessor) {
170+
protected Object doExecute(AbstractJpaQuery repositoryQuery, JpaParametersParameterAccessor accessor) {
171171

172172
Query query = repositoryQuery.createQuery(accessor);
173173

@@ -294,41 +294,41 @@ protected Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccesso
294294
*/
295295
static class ProcedureExecution extends JpaQueryExecution {
296296

297+
private final boolean collectionQuery;
298+
297299
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";
298300

301+
ProcedureExecution(boolean collectionQuery) {
302+
this.collectionQuery = collectionQuery;
303+
}
304+
299305
@Override
300306
protected Object doExecute(AbstractJpaQuery jpaQuery, JpaParametersParameterAccessor accessor) {
301307

302308
Assert.isInstanceOf(StoredProcedureJpaQuery.class, jpaQuery);
303309

304-
StoredProcedureJpaQuery storedProcedureJpaQuery = (StoredProcedureJpaQuery) jpaQuery;
305-
306-
StoredProcedureQuery storedProcedure = storedProcedureJpaQuery.createQuery(accessor);
310+
StoredProcedureJpaQuery query = (StoredProcedureJpaQuery) jpaQuery;
311+
StoredProcedureQuery procedure = query.createQuery(accessor);
307312

308313
try {
309314

310-
boolean returnsResultSet = storedProcedure.execute();
315+
boolean returnsResultSet = procedure.execute();
311316

312317
if (returnsResultSet) {
313318

314319
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
315320
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
316321
}
317322

318-
if (storedProcedureJpaQuery.getQueryMethod().isCollectionQuery()) {
319-
return storedProcedure.getResultList();
320-
} else {
321-
return storedProcedure.getSingleResult();
322-
}
323+
return collectionQuery ? procedure.getResultList() : procedure.getSingleResult();
323324
}
324325

325-
return storedProcedureJpaQuery.extractOutputValue(storedProcedure);
326-
326+
return query.extractOutputValue(procedure);
327327
} finally {
328328

329-
if (storedProcedure instanceof AutoCloseable autoCloseable) {
329+
if (procedure instanceof AutoCloseable ac) {
330330
try {
331-
autoCloseable.close();
331+
ac.close();
332332
} catch (Exception ignored) {}
333333
}
334334
}
@@ -345,10 +345,10 @@ static class StreamExecution extends JpaQueryExecution {
345345

346346
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";
347347

348-
private static Method streamMethod = ReflectionUtils.findMethod(Query.class, "getResultStream");
348+
private static final Method streamMethod = ReflectionUtils.findMethod(Query.class, "getResultStream");
349349

350350
@Override
351-
protected Object doExecute(final AbstractJpaQuery query, JpaParametersParameterAccessor accessor) {
351+
protected Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccessor accessor) {
352352

353353
if (!SurroundingTransactionDetectorMethodInterceptor.INSTANCE.isSurroundingTransactionActive()) {
354354
throw new InvalidDataAccessApiUsageException(NO_SURROUNDING_TRANSACTION);
@@ -369,24 +369,4 @@ protected Object doExecute(final AbstractJpaQuery query, JpaParametersParameterA
369369
}
370370
}
371371

372-
/**
373-
* Removes the converter being able to convert any object into an {@link Optional} from the given
374-
* {@link ConversionService} in case we're running on Java 8.
375-
*
376-
* @param conversionService must not be {@literal null}.
377-
*/
378-
public static void potentiallyRemoveOptionalConverter(ConfigurableConversionService conversionService) {
379-
380-
ClassLoader classLoader = JpaQueryExecution.class.getClassLoader();
381-
382-
if (ClassUtils.isPresent("java.util.Optional", classLoader)) {
383-
384-
try {
385-
386-
Class<?> optionalType = ClassUtils.forName("java.util.Optional", classLoader);
387-
conversionService.removeConvertible(Object.class, optionalType);
388-
389-
} catch (ClassNotFoundException | LinkageError o_O) {}
390-
}
391-
}
392372
}

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

Lines changed: 0 additions & 1 deletion
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)