Skip to content

Commit bc575de

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 09b2afa commit bc575de

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

@@ -970,6 +971,8 @@ <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions o
970971
* fields specification. Must not be {@literal null}.
971972
* @param replacement the replacement document. Must not be {@literal null}.
972973
* @return the converted object that was updated or {@literal null}, if not found.
974+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
975+
* {@link #getCollectionName(Class) derived} from the given replacement value.
973976
* @since 2.1
974977
*/
975978
@Nullable
@@ -1011,6 +1014,8 @@ default <T> T findAndReplace(Query query, T replacement, String collectionName)
10111014
* @return the converted object that was updated or {@literal null}, if not found. Depending on the value of
10121015
* {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or
10131016
* as it is after the update.
1017+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1018+
* {@link #getCollectionName(Class) derived} from the given replacement value.
10141019
* @since 2.1
10151020
*/
10161021
@Nullable
@@ -1084,6 +1089,8 @@ default <T> T findAndReplace(Query query, T replacement, FindAndReplaceOptions o
10841089
* @return the converted object that was updated or {@literal null}, if not found. Depending on the value of
10851090
* {@link FindAndReplaceOptions#isReturnNew()} this will either be the object as it was before the update or
10861091
* as it is after the update.
1092+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1093+
* {@link #getCollectionName(Class) derived} from the given replacement value.
10871094
* @since 2.1
10881095
*/
10891096
@Nullable
@@ -1167,6 +1174,8 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
11671174
* {@literal null}.
11681175
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
11691176
* @return the count of matching documents.
1177+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1178+
* {@link #getCollectionName(Class) derived} from the given type.
11701179
* @see #exactCount(Query, Class)
11711180
* @see #estimatedCount(Class)
11721181
*/
@@ -1222,6 +1231,8 @@ <S, T> T findAndReplace(Query query, S replacement, FindAndReplaceOptions option
12221231
*
12231232
* @param entityClass must not be {@literal null}.
12241233
* @return the estimated number of documents.
1234+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1235+
* {@link #getCollectionName(Class) derived} from the given type.
12251236
* @since 3.1
12261237
*/
12271238
default long estimatedCount(Class<?> entityClass) {
@@ -1258,6 +1269,8 @@ default long estimatedCount(Class<?> entityClass) {
12581269
* {@literal null}.
12591270
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
12601271
* @return the count of matching documents.
1272+
* @throws org.springframework.data.mapping.MappingException if the collection name cannot be
1273+
* {@link #getCollectionName(Class) derived} from the given type.
12611274
* @since 3.4
12621275
*/
12631276
default long exactCount(Query query, Class<?> entityClass) {
@@ -1325,6 +1338,8 @@ default long exactCount(Query query, String collectionName) {
13251338
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
13261339
* @return the inserted object.
13271340
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
1341+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1342+
* {@link #getCollectionName(Class) derived} from the given object type.
13281343
*/
13291344
<T> T insert(T objectToSave);
13301345

@@ -1349,6 +1364,8 @@ default long exactCount(Query query, String collectionName) {
13491364
* @param batchToSave the batch of objects to save. Must not be {@literal null}.
13501365
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
13511366
* @return the inserted objects that.
1367+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1368+
* {@link #getCollectionName(Class) derived} from the given type.
13521369
*/
13531370
<T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass);
13541371

@@ -1367,6 +1384,8 @@ default long exactCount(Query query, String collectionName) {
13671384
*
13681385
* @param objectsToSave the list of objects to save. Must not be {@literal null}.
13691386
* @return the inserted objects.
1387+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1388+
* {@link #getCollectionName(Class) derived} for the given objects.
13701389
*/
13711390
<T> Collection<T> insertAll(Collection<? extends T> objectsToSave);
13721391

@@ -1385,6 +1404,8 @@ default long exactCount(Query query, String collectionName) {
13851404
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
13861405
* @return the saved object.
13871406
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
1407+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1408+
* {@link #getCollectionName(Class) derived} from the given object type.
13881409
*/
13891410
<T> T save(T objectToSave);
13901411

@@ -1419,9 +1440,11 @@ default long exactCount(Query query, String collectionName) {
14191440
* the existing object. Must not be {@literal null}.
14201441
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
14211442
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1422-
* @since 3.0
14231443
* @see Update
14241444
* @see AggregationUpdate
1445+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1446+
* {@link #getCollectionName(Class) derived} from the given type.
1447+
* @since 3.0
14251448
*/
14261449
UpdateResult upsert(Query query, UpdateDefinition update, Class<?> entityClass);
14271450

@@ -1473,9 +1496,11 @@ default long exactCount(Query query, String collectionName) {
14731496
* the existing. Must not be {@literal null}.
14741497
* @param entityClass class that determines the collection to use.
14751498
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1476-
* @since 3.0
14771499
* @see Update
14781500
* @see AggregationUpdate
1501+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1502+
* {@link #getCollectionName(Class) derived} from the given type.
1503+
* @since 3.0
14791504
*/
14801505
UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> entityClass);
14811506

@@ -1527,9 +1552,11 @@ default long exactCount(Query query, String collectionName) {
15271552
* the existing. Must not be {@literal null}.
15281553
* @param entityClass class of the pojo to be operated on. Must not be {@literal null}.
15291554
* @return the {@link UpdateResult} which lets you access the results of the previous write.
1530-
* @since 3.0
1555+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1556+
* {@link #getCollectionName(Class) derived} from the given type.
15311557
* @see Update
15321558
* @see AggregationUpdate
1559+
* @since 3.0
15331560
*/
15341561
UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass);
15351562

@@ -1577,6 +1604,8 @@ default long exactCount(Query query, String collectionName) {
15771604
*
15781605
* @param object must not be {@literal null}.
15791606
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
1607+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1608+
* {@link #getCollectionName(Class) derived} from the given object type.
15801609
*/
15811610
DeleteResult remove(Object object);
15821611

@@ -1600,6 +1629,8 @@ default long exactCount(Query query, String collectionName) {
16001629
* @param entityClass class that determines the collection to use.
16011630
* @return the {@link DeleteResult} which lets you access the results of the previous delete.
16021631
* @throws IllegalArgumentException when {@literal query} or {@literal entityClass} is {@literal null}.
1632+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1633+
* {@link #getCollectionName(Class) derived} from the given type.
16031634
*/
16041635
DeleteResult remove(Query query, Class<?> entityClass);
16051636

@@ -1647,6 +1678,8 @@ default long exactCount(Query query, String collectionName) {
16471678
* @param query the query document that specifies the criteria used to find and remove documents.
16481679
* @param entityClass class of the pojo to be operated on.
16491680
* @return the {@link List} converted objects deleted by this operation.
1681+
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
1682+
* {@link #getCollectionName(Class) derived} from the given type.
16501683
* @since 1.5
16511684
*/
16521685
<T> List<T> findAllAndRemove(Query query, Class<T> entityClass);

0 commit comments

Comments
 (0)