File tree Expand file tree Collapse file tree
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff line change 1515 */
1616package org .springframework .data .mongodb .core .mapping ;
1717
18+ import java .util .function .Function ;
19+
20+ import org .jspecify .annotations .Nullable ;
21+
1822import 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 */
Original file line number Diff line number Diff line change 1515 */
1616package org .springframework .data .mongodb .core .mapping ;
1717
18+ import java .util .function .Function ;
19+
20+ import org .jspecify .annotations .Nullable ;
21+
1822import org .springframework .core .annotation .MergedAnnotations ;
1923import org .springframework .data .mongodb .MongoCollectionUtils ;
2024import 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 ;
You can’t perform that action at this time.
0 commit comments