diff --git a/src/main/java/org/springframework/data/couchbase/repository/support/CrudMethodMetadataPostProcessor.java b/src/main/java/org/springframework/data/couchbase/repository/support/CrudMethodMetadataPostProcessor.java index 0e5dd19b..836db257 100644 --- a/src/main/java/org/springframework/data/couchbase/repository/support/CrudMethodMetadataPostProcessor.java +++ b/src/main/java/org/springframework/data/couchbase/repository/support/CrudMethodMetadataPostProcessor.java @@ -145,6 +145,7 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B currentInvocation.set(invocation); try { + CrudMethodMetadata metadata = (CrudMethodMetadata) TransactionSynchronizationManager.getResource(method); if (metadata != null) { @@ -155,7 +156,7 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B if (methodMetadata == null) { - methodMetadata = new DefaultCrudMethodMetadata(method); + methodMetadata = new DefaultCrudMethodMetadata(method, repositoryInformation); CrudMethodMetadata tmp = metadataCache.putIfAbsent(method, methodMetadata); if (tmp != null) { @@ -186,6 +187,7 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B private final Method method; private final ScanConsistency scanConsistency; + private final RepositoryInformation repositoryInformation; private final String scope; private final String collection; @@ -196,9 +198,10 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B * * @param method must not be {@literal null}. */ - DefaultCrudMethodMetadata(Method method) { + DefaultCrudMethodMetadata(Method method, RepositoryInformation repositoryInformation) { Assert.notNull(method, "Method must not be null!"); this.method = method; + this.repositoryInformation = repositoryInformation; String n = method.getName(); // internal methods if (n.equals("getEntityInformation") || n.equals("getOperations") || n.equals("withOptions") @@ -209,7 +212,8 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B return; } - AnnotatedElement[] annotated = new AnnotatedElement[] { method, method.getDeclaringClass()}; + AnnotatedElement[] annotated = new AnnotatedElement[] { method, method.getDeclaringClass(), + repositoryInformation.getRepositoryInterface(), repositoryInformation.getDomainType() }; this.scanConsistency = OptionsBuilder.annotation(ScanConsistency.class, "query", QueryScanConsistency.NOT_BOUNDED, annotated); this.scope = OptionsBuilder.annotationString(Scope.class, CollectionIdentifier.DEFAULT_SCOPE, annotated); diff --git a/src/test/java/org/springframework/data/couchbase/domain/UserColRepository.java b/src/test/java/org/springframework/data/couchbase/domain/UserColRepository.java index 412a5755..e1bbaa37 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/UserColRepository.java +++ b/src/test/java/org/springframework/data/couchbase/domain/UserColRepository.java @@ -38,9 +38,7 @@ import com.couchbase.client.java.query.QueryScanConsistency; @ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS) public interface UserColRepository extends CouchbaseRepository, DynamicProxyable { - // CouchbaseRepositoryQueryCollectionIntegrationTests.testScopeCollectionAnnotationSwap() relies on this - // being commented out. - // S save(S var1); + S save(S var1); List findByFirstname(String firstname); diff --git a/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java index 82d24e8d..f522671e 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java @@ -15,7 +15,6 @@ */ package org.springframework.data.couchbase.repository.query; -import static com.couchbase.client.core.io.CollectionIdentifier.DEFAULT_SCOPE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -179,7 +178,7 @@ public class CouchbaseRepositoryQueryCollectionIntegrationTests extends Collecti // scope List found = userColRepository.withCollection(otherCollection).findByFirstname(user.getFirstname()); assertEquals(saved, found.get(0), "should have found what was saved"); - List notfound = userColRepository.withScope(DEFAULT_SCOPE) + List notfound = userColRepository.withScope(CollectionIdentifier.DEFAULT_SCOPE) .withCollection(CollectionIdentifier.DEFAULT_COLLECTION).findByFirstname(user.getFirstname()); assertEquals(0, notfound.size(), "should not have found what was saved"); } finally { @@ -189,20 +188,6 @@ public class CouchbaseRepositoryQueryCollectionIntegrationTests extends Collecti } } - @Test - public void testScopeCollectionAnnotationSwap() { - // UserCol annotation scope is other_scope, collection is other_collection - // airportRepository relies on Config.setScopeName(scopeName) ("my_scope") from CollectionAwareIntegrationTests. - // using airportRepository without specified a collection should fail. - // This test ensures that airportRepository.save(airport) doesn't get the - // collection from CrudMethodMetadata of UserCol.save() - UserCol userCol = new UserCol("1", "Dave", "Wilson"); - Airport airport = new Airport("3", "myIata", "myIcao"); - UserCol savedCol = userColRepository.save(userCol); // uses UserCol annotation scope, populates CrudMethodMetadata - userColRepository.delete(userCol); // uses UserCol annotation scope, populates CrudMethodMetadata - assertThrows(IllegalStateException.class, () -> airportRepository.save(airport)); - } - // template default scope is my_scope // UserCol annotation scope is other_scope @Test @@ -213,7 +198,7 @@ public class CouchbaseRepositoryQueryCollectionIntegrationTests extends Collecti List found = userColRepository.withScope(scopeName).withCollection(collectionName) .findByFirstname(user.getFirstname()); assertEquals(saved, found.get(0), "should have found what was saved"); - List notfound = userColRepository.withScope(DEFAULT_SCOPE) + List notfound = userColRepository.withScope(CollectionIdentifier.DEFAULT_SCOPE) .withCollection(CollectionIdentifier.DEFAULT_COLLECTION).findByFirstname(user.getFirstname()); assertEquals(0, notfound.size(), "should not have found what was saved"); userColRepository.withScope(scopeName).withCollection(collectionName).delete(user);