|
20 | 20 | import lombok.extern.slf4j.Slf4j;
|
21 | 21 |
|
22 | 22 | import java.io.Serializable;
|
| 23 | +import java.lang.reflect.Method; |
23 | 24 | import java.util.Optional;
|
| 25 | +import java.util.stream.Stream; |
24 | 26 |
|
25 | 27 | import javax.persistence.EntityManager;
|
26 | 28 | import javax.persistence.Tuple;
|
|
46 | 48 | import org.springframework.data.repository.core.support.RepositoryComposition;
|
47 | 49 | import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
48 | 50 | import org.springframework.data.repository.core.support.RepositoryFragment;
|
| 51 | +import org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor; |
49 | 52 | import org.springframework.data.repository.query.QueryLookupStrategy;
|
50 | 53 | import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
51 | 54 | import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
|
52 | 55 | import org.springframework.data.repository.query.ReturnedType;
|
53 | 56 | import org.springframework.lang.Nullable;
|
54 | 57 | import org.springframework.util.Assert;
|
| 58 | +import org.springframework.util.ReflectionUtils; |
55 | 59 |
|
56 | 60 | /**
|
57 | 61 | * JPA specific generic repository factory.
|
@@ -86,6 +90,12 @@ public JpaRepositoryFactory(EntityManager entityManager) {
|
86 | 90 | this.entityPathResolver = SimpleEntityPathResolver.INSTANCE;
|
87 | 91 |
|
88 | 92 | addRepositoryProxyPostProcessor(crudMethodMetadataPostProcessor);
|
| 93 | + addRepositoryProxyPostProcessor((factory, repositoryInformation) -> { |
| 94 | + |
| 95 | + if (hasMethodReturningStream(repositoryInformation.getRepositoryInterface())) { |
| 96 | + factory.addAdvice(SurroundingTransactionDetectorMethodInterceptor.INSTANCE); |
| 97 | + } |
| 98 | + }); |
89 | 99 |
|
90 | 100 | if (extractor.equals(PersistenceProvider.ECLIPSELINK)) {
|
91 | 101 | addQueryCreationListener(new EclipseLinkProjectionQueryCreationListener(entityManager));
|
@@ -231,6 +241,19 @@ protected RepositoryComposition.RepositoryFragments getRepositoryFragments(Repos
|
231 | 241 | return fragments;
|
232 | 242 | }
|
233 | 243 |
|
| 244 | + private static boolean hasMethodReturningStream(Class<?> repositoryClass) { |
| 245 | + |
| 246 | + Method[] methods = ReflectionUtils.getAllDeclaredMethods(repositoryClass); |
| 247 | + |
| 248 | + for (Method method : methods) { |
| 249 | + if (Stream.class.isAssignableFrom(method.getReturnType())) { |
| 250 | + return true; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + return false; |
| 255 | + } |
| 256 | + |
234 | 257 | /**
|
235 | 258 | * Query creation listener that informs EclipseLink users that they have to be extra careful when defining repository
|
236 | 259 | * query methods using projections as we have to rely on the declaration order of the accessors in projection
|
|
0 commit comments