Skip to content

Commit 776a853

Browse files
mp911dechristophstrobl
authored andcommitted
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
1 parent fc8aa67 commit 776a853

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.support;
1717

18-
import java.io.Serializable;
1918
import java.lang.reflect.Method;
2019
import java.util.Optional;
2120

@@ -120,14 +119,13 @@ protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata
120119
* @since 3.2.1
121120
*/
122121
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata, MongoOperations operations) {
123-
return fragmentsContributor.contribute(metadata, getEntityInformation(metadata.getDomainType()), operations);
122+
return fragmentsContributor.contribute(metadata, getEntityInformation(metadata), operations);
124123
}
125124

126125
@Override
127126
protected Object getTargetRepository(RepositoryInformation information) {
128127

129-
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType(),
130-
information);
128+
MongoEntityInformation<?, ?> entityInformation = getEntityInformation(information);
131129
Object targetRepository = getTargetRepositoryViaReflection(information, entityInformation, operations);
132130

133131
if (targetRepository instanceof SimpleMongoRepository<?, ?> repository) {
@@ -143,16 +141,18 @@ protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable Key key
143141
return Optional.of(new MongoQueryLookupStrategy(operations, mappingContext, valueExpressionDelegate));
144142
}
145143

144+
@Deprecated
145+
@Override
146146
public <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
147-
return getEntityInformation(domainClass, null);
147+
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(domainClass);
148+
return MongoEntityInformationSupport.entityInformationFor(entity, null);
148149
}
149150

150-
private <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass,
151-
@Nullable RepositoryMetadata metadata) {
151+
@Override
152+
public MongoEntityInformation<?, ?> getEntityInformation(RepositoryMetadata metadata) {
152153

153-
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(domainClass);
154-
return MongoEntityInformationSupport.<T, ID> entityInformationFor(entity,
155-
metadata != null ? metadata.getIdType() : null);
154+
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(metadata.getDomainType());
155+
return MongoEntityInformationSupport.entityInformationFor(entity, metadata.getIdType());
156156
}
157157

158158
/**

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

+9-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.support;
1717

18-
import java.io.Serializable;
1918
import java.lang.reflect.Method;
2019
import java.util.Optional;
2120

@@ -118,14 +117,14 @@ protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
118117
*/
119118
@Override
120119
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) {
121-
return fragmentsContributor.contribute(metadata, getEntityInformation(metadata.getDomainType(), metadata),
120+
return fragmentsContributor.contribute(metadata, getEntityInformation(metadata),
122121
operations);
123122
}
124123

125124
@Override
126125
protected Object getTargetRepository(RepositoryInformation information) {
127126

128-
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType(),
127+
MongoEntityInformation<?, ?> entityInformation = getEntityInformation(
129128
information);
130129
Object targetRepository = getTargetRepositoryViaReflection(information, entityInformation, operations);
131130

@@ -143,19 +142,18 @@ protected Optional<QueryLookupStrategy> getQueryLookupStrategy(Key key,
143142
return Optional.of(new MongoQueryLookupStrategy(operations, mappingContext, valueExpressionDelegate));
144143
}
145144

145+
@Deprecated
146146
@Override
147147
public <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
148-
return getEntityInformation(domainClass, null);
148+
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(domainClass);
149+
return MongoEntityInformationSupport.entityInformationFor(entity, null);
149150
}
150151

151-
@SuppressWarnings("unchecked")
152-
private <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass,
153-
@Nullable RepositoryMetadata metadata) {
154-
155-
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(domainClass);
152+
@Override
153+
public MongoEntityInformation<?, ?> getEntityInformation(RepositoryMetadata metadata) {
156154

157-
return new MappingMongoEntityInformation<>((MongoPersistentEntity<T>) entity,
158-
metadata != null ? (Class<ID>) metadata.getIdType() : null);
155+
MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(metadata.getDomainType());
156+
return MongoEntityInformationSupport.entityInformationFor(entity, metadata.getIdType());
159157
}
160158

161159
/**

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ public void publishesEventsForQuerydslFindQueries() {
408408
template.save(new Person("Boba", "Fett", 40));
409409

410410
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
411-
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
411+
MongoEntityInformation<Person, String> entityInformation = factory
412+
.getEntityInformation(Person.class);
412413
QuerydslMongoPredicateExecutor executor = new QuerydslMongoPredicateExecutor<>(entityInformation, template);
413414

414415
executor.findOne(QPerson.person.lastname.startsWith("Fe"));

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,16 @@
3131
import org.mockito.junit.jupiter.MockitoSettings;
3232
import org.mockito.quality.Strictness;
3333

34-
import org.springframework.data.mapping.context.MappingContext;
3534
import org.springframework.data.mongodb.core.MongoOperations;
36-
import org.springframework.data.mongodb.core.MongoTemplate;
3735
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
3836
import org.springframework.data.mongodb.core.convert.MongoConverter;
3937
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
4038
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
4139
import org.springframework.data.mongodb.core.query.Query;
4240
import org.springframework.data.mongodb.repository.Person;
4341
import org.springframework.data.mongodb.repository.ReadPreference;
44-
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
4542
import org.springframework.data.repository.ListCrudRepository;
46-
import org.springframework.data.repository.Repository;
43+
import org.springframework.data.repository.core.EntityInformation;
4744

4845
/**
4946
* Unit test for {@link MongoRepositoryFactory}.
@@ -69,7 +66,7 @@ public void setUp() {
6966
public void usesMappingMongoEntityInformationIfMappingContextSet() {
7067

7168
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
72-
MongoEntityInformation<Person, Serializable> entityInformation = factory.getEntityInformation(Person.class);
69+
EntityInformation<Person, Serializable> entityInformation = factory.getEntityInformation(Person.class);
7370
assertThat(entityInformation instanceof MappingMongoEntityInformation).isTrue();
7471
}
7572

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public class QuerydslMongoPredicateExecutorIntegrationTests {
7575
public void setup() {
7676

7777
MongoRepositoryFactory factory = new MongoRepositoryFactory(operations);
78-
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
78+
MongoEntityInformation<Person, String> entityInformation = factory
79+
.getEntityInformation(Person.class);
7980
repository = new QuerydslMongoPredicateExecutor<>(entityInformation, operations);
8081

8182
operations.dropCollection(Person.class);
@@ -246,7 +247,8 @@ protected MongoDatabase doGetDatabase() {
246247
};
247248

248249
MongoRepositoryFactory factory = new MongoRepositoryFactory(ops);
249-
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
250+
MongoEntityInformation<Person, String> entityInformation = factory
251+
.getEntityInformation(Person.class);
250252
repository = new QuerydslMongoPredicateExecutor<>(entityInformation, ops);
251253

252254
repository.findOne(person.firstname.contains("batman"));

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public static void cleanDb() {
111111
public void setup() {
112112

113113
ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(operations);
114-
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
114+
MongoEntityInformation<Person, String> entityInformation = factory
115+
.getEntityInformation(Person.class);
115116
repository = new ReactiveQuerydslMongoPredicateExecutor<>(entityInformation, operations);
116117

117118
dave = new Person("Dave", "Matthews", 42);
@@ -326,7 +327,8 @@ protected Mono<MongoDatabase> doGetDatabase() {
326327
};
327328

328329
ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(ops);
329-
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
330+
MongoEntityInformation<Person, String> entityInformation = factory
331+
.getEntityInformation(Person.class);
330332
repository = new ReactiveQuerydslMongoPredicateExecutor<>(entityInformation, ops);
331333

332334
repository.findOne(person.firstname.contains("batman")) //

0 commit comments

Comments
 (0)