Browse Source

Implement `RepositoryFactorySupport.getEntityInformation(RepositoryMetadata)` instead of private overload.

Overriding the proper variant of EntityInformation is now possible because we no longer utilize a private method in addition to the public one leading to partial customization of EntityInformation.

Closes #4967
pull/4976/head
Mark Paluch 7 months ago
parent
commit
b0d8a55cd0
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 20
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java
  2. 20
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactory.java
  3. 3
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java
  4. 7
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java
  5. 6
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/QuerydslMongoPredicateExecutorIntegrationTests.java
  6. 6
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveQuerydslMongoPredicateExecutorTests.java

20
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java

@ -15,7 +15,6 @@ @@ -15,7 +15,6 @@
*/
package org.springframework.data.mongodb.repository.support;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Optional;
@ -120,14 +119,13 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { @@ -120,14 +119,13 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
* @since 3.2.1
*/
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata, MongoOperations operations) {
return fragmentsContributor.contribute(metadata, getEntityInformation(metadata.getDomainType()), operations);
return fragmentsContributor.contribute(metadata, getEntityInformation(metadata), operations);
}
@Override
protected Object getTargetRepository(RepositoryInformation information) {
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType(),
information);
MongoEntityInformation<?, ?> entityInformation = getEntityInformation(information);
Object targetRepository = getTargetRepositoryViaReflection(information, entityInformation, operations);
if (targetRepository instanceof SimpleMongoRepository<?, ?> repository) {
@ -143,16 +141,18 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { @@ -143,16 +141,18 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
return Optional.of(new MongoQueryLookupStrategy(operations, mappingContext, valueExpressionDelegate));
}
@Deprecated
@Override
public <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
return getEntityInformation(domainClass, null);
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(domainClass);
return MongoEntityInformationSupport.entityInformationFor(entity, null);
}
private <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass,
@Nullable RepositoryMetadata metadata) {
@Override
public MongoEntityInformation<?, ?> getEntityInformation(RepositoryMetadata metadata) {
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(domainClass);
return MongoEntityInformationSupport.<T, ID> entityInformationFor(entity,
metadata != null ? metadata.getIdType() : null);
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(metadata.getDomainType());
return MongoEntityInformationSupport.entityInformationFor(entity, metadata.getIdType());
}
/**

20
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactory.java

@ -15,7 +15,6 @@ @@ -15,7 +15,6 @@
*/
package org.springframework.data.mongodb.repository.support;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Optional;
@ -118,14 +117,14 @@ public class ReactiveMongoRepositoryFactory extends ReactiveRepositoryFactorySup @@ -118,14 +117,14 @@ public class ReactiveMongoRepositoryFactory extends ReactiveRepositoryFactorySup
*/
@Override
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) {
return fragmentsContributor.contribute(metadata, getEntityInformation(metadata.getDomainType(), metadata),
return fragmentsContributor.contribute(metadata, getEntityInformation(metadata),
operations);
}
@Override
protected Object getTargetRepository(RepositoryInformation information) {
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType(),
MongoEntityInformation<?, ?> entityInformation = getEntityInformation(
information);
Object targetRepository = getTargetRepositoryViaReflection(information, entityInformation, operations);
@ -143,19 +142,18 @@ public class ReactiveMongoRepositoryFactory extends ReactiveRepositoryFactorySup @@ -143,19 +142,18 @@ public class ReactiveMongoRepositoryFactory extends ReactiveRepositoryFactorySup
return Optional.of(new MongoQueryLookupStrategy(operations, mappingContext, valueExpressionDelegate));
}
@Deprecated
@Override
public <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
return getEntityInformation(domainClass, null);
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(domainClass);
return MongoEntityInformationSupport.entityInformationFor(entity, null);
}
@SuppressWarnings("unchecked")
private <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass,
@Nullable RepositoryMetadata metadata) {
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(domainClass);
@Override
public MongoEntityInformation<?, ?> getEntityInformation(RepositoryMetadata metadata) {
return new MappingMongoEntityInformation<>((MongoPersistentEntity<T>) entity,
metadata != null ? (Class<ID>) metadata.getIdType() : null);
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(metadata.getDomainType());
return MongoEntityInformationSupport.entityInformationFor(entity, metadata.getIdType());
}
/**

3
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ApplicationContextEventTests.java

@ -408,7 +408,8 @@ public class ApplicationContextEventTests { @@ -408,7 +408,8 @@ public class ApplicationContextEventTests {
template.save(new Person("Boba", "Fett", 40));
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
MongoEntityInformation<Person, String> entityInformation = factory
.getEntityInformation(Person.class);
QuerydslMongoPredicateExecutor executor = new QuerydslMongoPredicateExecutor<>(entityInformation, template);
executor.findOne(QPerson.person.lastname.startsWith("Fe"));

7
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java

@ -31,9 +31,7 @@ import org.mockito.junit.jupiter.MockitoExtension; @@ -31,9 +31,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
@ -41,9 +39,8 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext; @@ -41,9 +39,8 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.Person;
import org.springframework.data.mongodb.repository.ReadPreference;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.EntityInformation;
/**
* Unit test for {@link MongoRepositoryFactory}.
@ -69,7 +66,7 @@ public class MongoRepositoryFactoryUnitTests { @@ -69,7 +66,7 @@ public class MongoRepositoryFactoryUnitTests {
public void usesMappingMongoEntityInformationIfMappingContextSet() {
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
MongoEntityInformation<Person, Serializable> entityInformation = factory.getEntityInformation(Person.class);
EntityInformation<Person, Serializable> entityInformation = factory.getEntityInformation(Person.class);
assertThat(entityInformation instanceof MappingMongoEntityInformation).isTrue();
}

6
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/QuerydslMongoPredicateExecutorIntegrationTests.java

@ -75,7 +75,8 @@ public class QuerydslMongoPredicateExecutorIntegrationTests { @@ -75,7 +75,8 @@ public class QuerydslMongoPredicateExecutorIntegrationTests {
public void setup() {
MongoRepositoryFactory factory = new MongoRepositoryFactory(operations);
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
MongoEntityInformation<Person, String> entityInformation = factory
.getEntityInformation(Person.class);
repository = new QuerydslMongoPredicateExecutor<>(entityInformation, operations);
operations.dropCollection(Person.class);
@ -246,7 +247,8 @@ public class QuerydslMongoPredicateExecutorIntegrationTests { @@ -246,7 +247,8 @@ public class QuerydslMongoPredicateExecutorIntegrationTests {
};
MongoRepositoryFactory factory = new MongoRepositoryFactory(ops);
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
MongoEntityInformation<Person, String> entityInformation = factory
.getEntityInformation(Person.class);
repository = new QuerydslMongoPredicateExecutor<>(entityInformation, ops);
repository.findOne(person.firstname.contains("batman"));

6
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveQuerydslMongoPredicateExecutorTests.java

@ -111,7 +111,8 @@ public class ReactiveQuerydslMongoPredicateExecutorTests { @@ -111,7 +111,8 @@ public class ReactiveQuerydslMongoPredicateExecutorTests {
public void setup() {
ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(operations);
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
MongoEntityInformation<Person, String> entityInformation = factory
.getEntityInformation(Person.class);
repository = new ReactiveQuerydslMongoPredicateExecutor<>(entityInformation, operations);
dave = new Person("Dave", "Matthews", 42);
@ -326,7 +327,8 @@ public class ReactiveQuerydslMongoPredicateExecutorTests { @@ -326,7 +327,8 @@ public class ReactiveQuerydslMongoPredicateExecutorTests {
};
ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(ops);
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
MongoEntityInformation<Person, String> entityInformation = factory
.getEntityInformation(Person.class);
repository = new ReactiveQuerydslMongoPredicateExecutor<>(entityInformation, ops);
repository.findOne(person.firstname.contains("batman")) //

Loading…
Cancel
Save