Skip to content

Commit 018fe62

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 27e6b5a commit 018fe62

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ public String determineCollectionName(@Nullable Class<?> entityClass) {
138138
"No class parameter provided, entity collection can't be determined!");
139139
}
140140

141-
return context.getRequiredPersistentEntity(entityClass).getCollection();
141+
MongoPersistentEntity<?> persistentEntity = context.getPersistentEntity(entityClass);
142+
if(persistentEntity == null) {
143+
throw new MappingException(String.format("Collection name cannot be derived for type %s. Is it a store native type?", entityClass));
144+
}
145+
return persistentEntity.getCollection();
142146
}
143147

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

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

+34-3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public interface MongoOperations extends FluentMongoOperations {
8080
*
8181
* @param entityClass must not be {@literal null}.
8282
* @return never {@literal null}.
83+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be derived from the type.
8384
*/
8485
String getCollectionName(Class<?> entityClass);
8586

@@ -968,6 +969,8 @@ <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions o
968969
* fields specification. Must not be {@literal null}.
969970
* @param replacement the replacement document. Must not be {@literal null}.
970971
* @return the converted object that was updated or {@literal null}, if not found.
972+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
973+
* {@link #getCollectionName(Class) derived} from the given replacement value.
971974
* @since 2.1
972975
*/
973976
@Nullable
@@ -1009,6 +1012,8 @@ default <T> T findAndReplace(Query query, T replacement, String collectionName)
10091012
* @return the converted object that was updated or {@literal null}, if not found. Depending on the value of
10101013
* {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or
10111014
* as it is after the update.
1015+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1016+
* {@link #getCollectionName(Class) derived} from the given replacement value.
10121017
* @since 2.1
10131018
*/
10141019
@Nullable
@@ -1082,6 +1087,8 @@ default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions o
10821087
* @return the converted object that was updated or {@literal null}, if not found. Depending on the value of
10831088
* {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or
10841089
* as it is after the update.
1090+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1091+
* {@link #getCollectionName(Class) derived} from the given replacement value.
10851092
* @since 2.1
10861093
*/
10871094
@Nullable
@@ -1171,6 +1178,8 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11711178
* {@literal null}.
11721179
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
11731180
* @return the count of matching documents.
1181+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1182+
* {@link #getCollectionName(Class) derived} from the given type.
11741183
*/
11751184
long count(Query query, Class<?> entityClass);
11761185

@@ -1205,6 +1214,8 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
12051214
*
12061215
* @param entityClass must not be {@literal null}.
12071216
* @return the estimated number of documents.
1217+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1218+
* {@link #getCollectionName(Class) derived} from the given type.
12081219
* @since 3.1
12091220
*/
12101221
default long estimatedCount(Class<?> entityClass) {
@@ -1265,6 +1276,8 @@ default long estimatedCount(Class<?> entityClass) {
12651276
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
12661277
* @return the inserted object.
12671278
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
1279+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1280+
* {@link #getCollectionName(Class) derived} from the given object type.
12681281
*/
12691282
<T> T insert(T objectToSave);
12701283

@@ -1291,6 +1304,8 @@ default long estimatedCount(Class<?> entityClass) {
12911304
* @param batchToSave the batch of objects to save. Must not be {@literal null}.
12921305
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
12931306
* @return the inserted objects that.
1307+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1308+
* {@link #getCollectionName(Class) derived} from the given type.
12941309
*/
12951310
<T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass);
12961311

@@ -1309,6 +1324,8 @@ default long estimatedCount(Class<?> entityClass) {
13091324
*
13101325
* @param objectsToSave the list of objects to save. Must not be {@literal null}.
13111326
* @return the inserted objects.
1327+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1328+
* {@link #getCollectionName(Class) derived} for the given objects.
13121329
*/
13131330
<T> Collection<T> insertAll(Collection<? extends T> objectsToSave);
13141331

@@ -1330,6 +1347,8 @@ default long estimatedCount(Class<?> entityClass) {
13301347
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
13311348
* @return the saved object.
13321349
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
1350+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1351+
* {@link #getCollectionName(Class) derived} from the given object type.
13331352
*/
13341353
<T> T save(T objectToSave);
13351354

@@ -1366,9 +1385,11 @@ default long estimatedCount(Class<?> entityClass) {
13661385
* the existing object. Must not be {@literal null}.
13671386
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
13681387
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1369-
* @since 3.0
13701388
* @see Update
13711389
* @see AggregationUpdate
1390+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1391+
* {@link #getCollectionName(Class) derived} from the given type.
1392+
* @since 3.0
13721393
*/
13731394
UpdateResult upsert(Query query, UpdateDefinition update, Class<?> entityClass);
13741395

@@ -1420,9 +1441,11 @@ default long estimatedCount(Class<?> entityClass) {
14201441
* the existing. Must not be {@literal null}.
14211442
* @param entityClass class that determines the collection to use.
14221443
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1423-
* @since 3.0
14241444
* @see Update
14251445
* @see AggregationUpdate
1446+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1447+
* {@link #getCollectionName(Class) derived} from the given type.
1448+
* @since 3.0
14261449
*/
14271450
UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> entityClass);
14281451

@@ -1474,9 +1497,11 @@ default long estimatedCount(Class<?> entityClass) {
14741497
* the existing. Must not be {@literal null}.
14751498
* @param entityClass class of the pojo to be operated on. Must not be {@literal null}.
14761499
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1477-
* @since 3.0
1500+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1501+
* {@link #getCollectionName(Class) derived} from the given type.
14781502
* @see Update
14791503
* @see AggregationUpdate
1504+
* @since 3.0
14801505
*/
14811506
UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass);
14821507

@@ -1524,6 +1549,8 @@ default long estimatedCount(Class<?> entityClass) {
15241549
*
15251550
* @param object must not be {@literal null}.
15261551
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
1552+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1553+
* {@link #getCollectionName(Class) derived} from the given object type.
15271554
*/
15281555
DeleteResult remove(Object object);
15291556

@@ -1547,6 +1574,8 @@ default long estimatedCount(Class<?> entityClass) {
15471574
* @param entityClass class that determines the collection to use.
15481575
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
15491576
* @throws IllegalArgumentException when {@literal query} or {@literal entityClass} is {@literal null}.
1577+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1578+
* {@link #getCollectionName(Class) derived} from the given type.
15501579
*/
15511580
DeleteResult remove(Query query, Class<?> entityClass);
15521581

@@ -1594,6 +1623,8 @@ default long estimatedCount(Class<?> entityClass) {
15941623
* @param query the query document that specifies the criteria used to find and remove documents.
15951624
* @param entityClass class of the pojo to be operated on.
15961625
* @return the {@link List} converted objects deleted by this operation.
1626+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1627+
* {@link #getCollectionName(Class) derived} from the given type.
15971628
* @since 1.5
15981629
*/
15991630
<T> List<T> findAllAndRemove(Query query, Class<T> entityClass);

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

+40-1
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ <T> Mono<T> findAndModify(Query query, UpdateDefinition update, FindAndModifyOpt
756756
* fields specification. Must not be {@literal null}.
757757
* @param replacement the replacement document. Must not be {@literal null}.
758758
* @return the converted object that was updated or {@link Mono#empty()}, if not found.
759+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
760+
* {@link #getCollectionName(Class) derived} from the given replacement value.
759761
* @since 2.1
760762
*/
761763
default <T> Mono<T> findAndReplace(Query query, T replacement) {
@@ -795,6 +797,8 @@ default <T> Mono<T> findAndReplace(Query query, T replacement, String collection
795797
* @return the converted object that was updated or {@link Mono#empty()}, if not found. Depending on the value of
796798
* {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or
797799
* as it is after the update.
800+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
801+
* {@link #getCollectionName(Class) derived} from the given replacement value.
798802
* @since 2.1
799803
*/
800804
default <T> Mono<T> findAndReplace(Query query, T replacement, FindAndReplaceOptions options) {
@@ -865,6 +869,8 @@ default <T> Mono<T> findAndReplace(Query query, T replacement, FindAndReplaceOpt
865869
* @return the converted object that was updated or {@link Mono#empty()}, if not found. Depending on the value of
866870
* {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or
867871
* as it is after the update.
872+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
873+
* {@link #getCollectionName(Class) derived} from the given replacement value.
868874
* @since 2.1
869875
*/
870876
default <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions options, Class<S> entityType,
@@ -951,6 +957,8 @@ <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions
951957
* {@literal null}.
952958
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
953959
* @return the count of matching documents.
960+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
961+
* {@link #getCollectionName(Class) derived} from the given type.
954962
*/
955963
Mono<Long> count(Query query, Class<?> entityClass);
956964

@@ -1007,6 +1015,8 @@ <S, T> Mono<T> findAndReplace(Query query, S replacement, FindAndReplaceOptions
10071015
*
10081016
* @param entityClass must not be {@literal null}.
10091017
* @return a {@link Mono} emitting the estimated number of documents.
1018+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1019+
* {@link #getCollectionName(Class) derived} from the given type.
10101020
* @since 3.1
10111021
*/
10121022
default Mono<Long> estimatedCount(Class<?> entityClass) {
@@ -1045,6 +1055,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
10451055
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
10461056
* @return the inserted object.
10471057
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
1058+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1059+
* {@link #getCollectionName(Class) derived} from the given object type.
10481060
*/
10491061
<T> Mono<T> insert(T objectToSave);
10501062

@@ -1070,7 +1082,9 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
10701082
*
10711083
* @param batchToSave the batch of objects to save. Must not be {@literal null}.
10721084
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
1073-
* @return the inserted objects .
1085+
* @return the inserted objects.
1086+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1087+
* {@link #getCollectionName(Class) derived} from the given type.
10741088
*/
10751089
<T> Flux<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass);
10761090

@@ -1089,6 +1103,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
10891103
*
10901104
* @param objectsToSave the list of objects to save. Must not be {@literal null}.
10911105
* @return the saved objects.
1106+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1107+
* {@link #getCollectionName(Class) derived} for the given objects.
10921108
*/
10931109
<T> Flux<T> insertAll(Collection<? extends T> objectsToSave);
10941110

@@ -1116,6 +1132,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
11161132
* @param batchToSave the publisher which provides objects to save. Must not be {@literal null}.
11171133
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
11181134
* @return the inserted objects.
1135+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1136+
* {@link #getCollectionName(Class) derived} for the type.
11191137
*/
11201138
<T> Flux<T> insertAll(Mono<? extends Collection<? extends T>> batchToSave, Class<?> entityClass);
11211139

@@ -1155,6 +1173,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
11551173
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
11561174
* @return the saved object.
11571175
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
1176+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1177+
* {@link #getCollectionName(Class) derived} from the given object type.
11581178
*/
11591179
<T> Mono<T> save(T objectToSave);
11601180

@@ -1191,6 +1211,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
11911211
*
11921212
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
11931213
* @return the saved object.
1214+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1215+
* {@link #getCollectionName(Class) derived} from the given object type.
11941216
*/
11951217
<T> Mono<T> save(Mono<? extends T> objectToSave);
11961218

@@ -1224,6 +1246,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
12241246
* the existing object. Must not be {@literal null}.
12251247
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
12261248
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1249+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1250+
* {@link #getCollectionName(Class) derived} from the given type.
12271251
* @since 3.0
12281252
* @see Update
12291253
* @see AggregationUpdate
@@ -1278,6 +1302,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
12781302
* the existing. Must not be {@literal null}.
12791303
* @param entityClass class that determines the collection to use.
12801304
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1305+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1306+
* {@link #getCollectionName(Class) derived} from the given type.
12811307
* @since 3.0
12821308
* @see Update
12831309
* @see AggregationUpdate
@@ -1333,6 +1359,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
13331359
* @param entityClass class of the pojo to be operated on. Must not be {@literal null}.
13341360
* @return the {@link UpdateResult} which lets you access the results of the previous write.
13351361
* @since 3.0
1362+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1363+
* {@link #getCollectionName(Class) derived} from the given type.
13361364
* @see Update
13371365
* @see AggregationUpdate
13381366
*/
@@ -1379,6 +1407,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
13791407
*
13801408
* @param object must not be {@literal null}.
13811409
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
1410+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1411+
* {@link #getCollectionName(Class) derived} from the given object type.
13821412
*/
13831413
Mono<DeleteResult> remove(Object object);
13841414

@@ -1396,6 +1426,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
13961426
*
13971427
* @param objectToRemove must not be {@literal null}.
13981428
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
1429+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1430+
* {@link #getCollectionName(Class) derived} from the given object type.
13991431
*/
14001432
Mono<DeleteResult> remove(Mono<? extends Object> objectToRemove);
14011433

@@ -1415,6 +1447,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
14151447
* @param query the query document that specifies the criteria used to remove a record.
14161448
* @param entityClass class that determines the collection to use.
14171449
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
1450+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1451+
* {@link #getCollectionName(Class) derived} from the given type.
14181452
*/
14191453
Mono<DeleteResult> remove(Query query, Class<?> entityClass);
14201454

@@ -1458,6 +1492,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
14581492
* @param query the query document that specifies the criteria used to find and remove documents.
14591493
* @param entityClass class of the pojo to be operated on.
14601494
* @return the {@link Flux} converted objects deleted by this operation.
1495+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1496+
* {@link #getCollectionName(Class) derived} from the given type.
14611497
*/
14621498
<T> Flux<T> findAllAndRemove(Query query, Class<T> entityClass);
14631499

@@ -1489,6 +1525,8 @@ default Mono<Long> estimatedCount(Class<?> entityClass) {
14891525
* specification.
14901526
* @param entityClass the parametrized type of the returned {@link Flux}.
14911527
* @return the {@link Flux} of converted objects.
1528+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1529+
* {@link #getCollectionName(Class) derived} from the given type.
14921530
*/
14931531
<T> Flux<T> tail(Query query, Class<T> entityClass);
14941532

@@ -1633,6 +1671,7 @@ <T> Flux<T> mapReduce(Query filterQuery, Class<?> domainType, String inputCollec
16331671
*
16341672
* @param entityClass must not be {@literal null}.
16351673
* @return never {@literal null}.
1674+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be derived from the type.
16361675
* @since 2.1
16371676
*/
16381677
String getCollectionName(Class<?> entityClass);

0 commit comments

Comments
 (0)