Skip to content

Commit ac538c6

Browse files
christophstroblmp911de
authored andcommitted
Improve exception message when deriving collection name from type.
We now provide a better worded exception message when trying to derive the collection name for a type that is not considered a user types (such as org.bson.Document). Update the Javadoc to hint to the error. Closes #4061 Original pull request: #4062.
1 parent 7b30423 commit ac538c6

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ public String determineCollectionName(@Nullable Class<?> entityClass) {
174174
"No class parameter provided, entity collection can't be determined!");
175175
}
176176

177-
return context.getRequiredPersistentEntity(entityClass).getCollection();
177+
MongoPersistentEntity<?> persistentEntity = context.getPersistentEntity(entityClass);
178+
if(persistentEntity == null) {
179+
throw new MappingException(String.format("Collection name cannot be derived for type %s. Is it a store native type?", entityClass));
180+
}
181+
return persistentEntity.getCollection();
178182
}
179183

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

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public interface MongoOperations extends FluentMongoOperations {
7979
*
8080
* @param entityClass must not be {@literal null}.
8181
* @return never {@literal null}.
82+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be derived from the type.
8283
*/
8384
String getCollectionName(Class<?> entityClass);
8485

@@ -947,6 +948,8 @@ <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions o
947948
* fields specification. Must not be {@literal null}.
948949
* @param replacement the replacement document. Must not be {@literal null}.
949950
* @return the converted object that was updated or {@literal null}, if not found.
951+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
952+
* {@link #getCollectionName(Class) derived} from the given replacement value.
950953
* @since 2.1
951954
*/
952955
@Nullable
@@ -988,6 +991,8 @@ default <T> T findAndReplace(Query query, T replacement, String collectionName)
988991
* @return the converted object that was updated or {@literal null}, if not found. Depending on the value of
989992
* {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or
990993
* as it is after the update.
994+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
995+
* {@link #getCollectionName(Class) derived} from the given replacement value.
991996
* @since 2.1
992997
*/
993998
@Nullable
@@ -1061,6 +1066,8 @@ default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions o
10611066
* @return the converted object that was updated or {@literal null}, if not found. Depending on the value of
10621067
* {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or
10631068
* as it is after the update.
1069+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1070+
* {@link #getCollectionName(Class) derived} from the given replacement value.
10641071
* @since 2.1
10651072
*/
10661073
@Nullable
@@ -1144,6 +1151,8 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11441151
* {@literal null}.
11451152
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
11461153
* @return the count of matching documents.
1154+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1155+
* {@link #getCollectionName(Class) derived} from the given type.
11471156
* @see #exactCount(Query, Class)
11481157
* @see #estimatedCount(Class)
11491158
*/
@@ -1199,6 +1208,8 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11991208
*
12001209
* @param entityClass must not be {@literal null}.
12011210
* @return the estimated number of documents.
1211+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1212+
* {@link #getCollectionName(Class) derived} from the given type.
12021213
* @since 3.1
12031214
*/
12041215
default long estimatedCount(Class<?> entityClass) {
@@ -1235,6 +1246,8 @@ default long estimatedCount(Class<?> entityClass) {
12351246
* {@literal null}.
12361247
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
12371248
* @return the count of matching documents.
1249+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1250+
* {@link #getCollectionName(Class) derived} from the given type.
12381251
* @since 3.4
12391252
*/
12401253
default long exactCount(Query query, Class<?> entityClass) {
@@ -1302,6 +1315,8 @@ default long exactCount(Query query, String collectionName) {
13021315
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
13031316
* @return the inserted object.
13041317
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
1318+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1319+
* {@link #getCollectionName(Class) derived} from the given object type.
13051320
*/
13061321
<T> T insert(T objectToSave);
13071322

@@ -1326,6 +1341,8 @@ default long exactCount(Query query, String collectionName) {
13261341
* @param batchToSave the batch of objects to save. Must not be {@literal null}.
13271342
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
13281343
* @return the inserted objects that.
1344+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1345+
* {@link #getCollectionName(Class) derived} from the given type.
13291346
*/
13301347
<T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass);
13311348

@@ -1344,6 +1361,8 @@ default long exactCount(Query query, String collectionName) {
13441361
*
13451362
* @param objectsToSave the list of objects to save. Must not be {@literal null}.
13461363
* @return the inserted objects.
1364+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1365+
* {@link #getCollectionName(Class) derived} for the given objects.
13471366
*/
13481367
<T> Collection<T> insertAll(Collection<? extends T> objectsToSave);
13491368

@@ -1362,6 +1381,8 @@ default long exactCount(Query query, String collectionName) {
13621381
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
13631382
* @return the saved object.
13641383
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
1384+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1385+
* {@link #getCollectionName(Class) derived} from the given object type.
13651386
*/
13661387
<T> T save(T objectToSave);
13671388

@@ -1396,9 +1417,11 @@ default long exactCount(Query query, String collectionName) {
13961417
* the existing object. Must not be {@literal null}.
13971418
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
13981419
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1399-
* @since 3.0
14001420
* @see Update
14011421
* @see AggregationUpdate
1422+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1423+
* {@link #getCollectionName(Class) derived} from the given type.
1424+
* @since 3.0
14021425
*/
14031426
UpdateResult upsert(Query query, UpdateDefinition update, Class<?> entityClass);
14041427

@@ -1450,9 +1473,11 @@ default long exactCount(Query query, String collectionName) {
14501473
* the existing. Must not be {@literal null}.
14511474
* @param entityClass class that determines the collection to use.
14521475
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1453-
* @since 3.0
14541476
* @see Update
14551477
* @see AggregationUpdate
1478+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1479+
* {@link #getCollectionName(Class) derived} from the given type.
1480+
* @since 3.0
14561481
*/
14571482
UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> entityClass);
14581483

@@ -1504,9 +1529,11 @@ default long exactCount(Query query, String collectionName) {
15041529
* the existing. Must not be {@literal null}.
15051530
* @param entityClass class of the pojo to be operated on. Must not be {@literal null}.
15061531
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1507-
* @since 3.0
1532+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1533+
* {@link #getCollectionName(Class) derived} from the given type.
15081534
* @see Update
15091535
* @see AggregationUpdate
1536+
* @since 3.0
15101537
*/
15111538
UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass);
15121539

@@ -1554,6 +1581,8 @@ default long exactCount(Query query, String collectionName) {
15541581
*
15551582
* @param object must not be {@literal null}.
15561583
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
1584+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1585+
* {@link #getCollectionName(Class) derived} from the given object type.
15571586
*/
15581587
DeleteResult remove(Object object);
15591588

@@ -1577,6 +1606,8 @@ default long exactCount(Query query, String collectionName) {
15771606
* @param entityClass class that determines the collection to use.
15781607
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
15791608
* @throws IllegalArgumentException when {@literal query} or {@literal entityClass} is {@literal null}.
1609+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1610+
* {@link #getCollectionName(Class) derived} from the given type.
15801611
*/
15811612
DeleteResult remove(Query query, Class<?> entityClass);
15821613

@@ -1624,6 +1655,8 @@ default long exactCount(Query query, String collectionName) {
16241655
* @param query the query document that specifies the criteria used to find and remove documents.
16251656
* @param entityClass class of the pojo to be operated on.
16261657
* @return the {@link List} converted objects deleted by this operation.
1658+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1659+
* {@link #getCollectionName(Class) derived} from the given type.
16271660
* @since 1.5
16281661
*/
16291662
<T> List<T> findAllAndRemove(Query query, Class<T> entityClass);

0 commit comments

Comments
 (0)