Skip to content

Commit 1e66755

Browse files
committed
Refine CollectionName API.
1 parent 217fbc6 commit 1e66755

4 files changed

Lines changed: 32 additions & 7 deletions

File tree

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/BulkWriterSupport.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,8 @@ String resolveCollectionName(BulkOperation operation) {
6969
String resolveCollectionName(TypedNamespace namespace) {
7070

7171
if (namespace.hasCollectionName()) {
72-
CollectionName collectionName = namespace.getRequiredCollectionName();
73-
if (collectionName.getEntityClass() == Object.class) {
74-
return collectionName.getCollectionName();
75-
}
76-
return entityOperations.determineCollectionName(collectionName.getEntityClass());
72+
return namespace.getRequiredCollectionName().getCollectionName(entityOperations::getRequiredPersistentEntity);
7773
}
78-
7974
return entityOperations.determineCollectionName(namespace.type());
8075
}
8176

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/EntityOperations.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,19 @@ public String determineCollectionName(@Nullable Class<?> entityClass) {
220220
"No class parameter provided, entity collection can't be determined");
221221
}
222222

223+
return getRequiredPersistentEntity(entityClass).getCollection();
224+
}
225+
226+
MongoPersistentEntity<?> getRequiredPersistentEntity(Class<?> entityClass) {
227+
223228
MongoPersistentEntity<?> persistentEntity = context.getPersistentEntity(entityClass);
224229

225230
if (persistentEntity == null) {
226231
throw new MappingException(String.format(
227232
"Cannot determine collection name from type '%s'. Is it a store native type?", entityClass.getName()));
228233
}
229234

230-
return persistentEntity.getCollection();
235+
return persistentEntity;
231236
}
232237

233238
public Query getByIdInQuery(Collection<?> entities) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CollectionName.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.springframework.data.mongodb.core.mapping;
1717

18+
import java.util.function.Function;
19+
20+
import org.jspecify.annotations.Nullable;
21+
1822
import org.springframework.util.Assert;
1923

2024
/**
@@ -31,6 +35,11 @@ public interface CollectionName {
3135
*/
3236
String getCollectionName();
3337

38+
/**
39+
* Returns the collection name, potentially by considering a {@link MongoPersistentEntity}.
40+
*/
41+
String getCollectionName(Function<Class<?>, @Nullable MongoPersistentEntity<?>> entityLookup);
42+
3443
/**
3544
* Returns the entity class for which the collection name is derived.
3645
*/

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CollectionNames.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.springframework.data.mongodb.core.mapping;
1717

18+
import java.util.function.Function;
19+
20+
import org.jspecify.annotations.Nullable;
21+
1822
import org.springframework.core.annotation.MergedAnnotations;
1923
import org.springframework.data.mongodb.MongoCollectionUtils;
2024
import org.springframework.util.ObjectUtils;
@@ -34,6 +38,11 @@ public String getCollectionName() {
3438
return collectionName;
3539
}
3640

41+
@Override
42+
public String getCollectionName(Function<Class<?>, @Nullable MongoPersistentEntity<?>> entityLookup) {
43+
return getCollectionName();
44+
}
45+
3746
@Override
3847
public Class<?> getEntityClass() {
3948
return Object.class;
@@ -67,6 +76,13 @@ public String getCollectionName() {
6776
.orElseGet(() -> MongoCollectionUtils.getPreferredCollectionName(entityClass));
6877
}
6978

79+
@Override
80+
public String getCollectionName(Function<Class<?>, @Nullable MongoPersistentEntity<?>> entityLookup) {
81+
82+
MongoPersistentEntity<?> entity = entityLookup.apply(getEntityClass());
83+
return entity != null ? entity.getCollection() : getCollectionName();
84+
}
85+
7086
@Override
7187
public Class<?> getEntityClass() {
7288
return entityClass;

0 commit comments

Comments
 (0)