Skip to content

Commit fcf3143

Browse files
christophstroblmp911de
authored andcommitted
Update documentation on version property handling.
Original pull request: #4553 Closes #4536
1 parent 5e76c04 commit fcf3143

File tree

3 files changed

+198
-18
lines changed

3 files changed

+198
-18
lines changed

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

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,9 @@ default <T> List<T> findDistinct(Query query, String field, String collection, C
945945
/**
946946
* Triggers <a href="https://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/">findAndModify </a>
947947
* to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query}.
948+
* <p>
949+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
950+
* incremented if not explicitly specified in the update.
948951
*
949952
* @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
950953
* optional fields specification. Must not be {@literal null}.
@@ -961,6 +964,9 @@ default <T> List<T> findDistinct(Query query, String field, String collection, C
961964
/**
962965
* Triggers <a href="https://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/">findAndModify </a>
963966
* to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query}.
967+
* <p>
968+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
969+
* incremented if not explicitly specified in the update.
964970
*
965971
* @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
966972
* optional fields specification. Must not be {@literal null}.
@@ -979,6 +985,9 @@ default <T> List<T> findDistinct(Query query, String field, String collection, C
979985
* Triggers <a href="https://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/">findAndModify </a>
980986
* to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query} taking
981987
* {@link FindAndModifyOptions} into account.
988+
* <p>
989+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
990+
* incremented if not explicitly specified in the update.
982991
*
983992
* @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
984993
* optional fields specification.
@@ -999,6 +1008,9 @@ default <T> List<T> findDistinct(Query query, String field, String collection, C
9991008
* Triggers <a href="https://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/">findAndModify </a>
10001009
* to apply provided {@link Update} on documents matching {@link Criteria} of given {@link Query} taking
10011010
* {@link FindAndModifyOptions} into account.
1011+
* <p>
1012+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
1013+
* incremented if not explicitly specified in the update.
10021014
*
10031015
* @param query the {@link Query} class that specifies the {@link Criteria} used to find a document and also an
10041016
* optional fields specification. Must not be {@literal null}.
@@ -1391,8 +1403,11 @@ default long exactCount(Query query, String collectionName) {
13911403
* leverages Type Conversion API. See
13921404
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
13931405
* Type Conversion"</a> for more details. <br />
1394-
* Insert is used to initially store the object into the database. To update an existing object use the save method.
1395-
* <br />
1406+
* Insert is used to initially store the object into the database. To update an existing object use the
1407+
* {@link #save(Object)} method.
1408+
* <p>
1409+
* Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization.
1410+
* <p>
13961411
* The {@code objectToSave} must not be collection-like.
13971412
*
13981413
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
@@ -1408,7 +1423,9 @@ default long exactCount(Query query, String collectionName) {
14081423
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless
14091424
* configured otherwise, an instance of {@link MappingMongoConverter} will be used. <br />
14101425
* Insert is used to initially store the object into the database. To update an existing object use the save method.
1411-
* <br />
1426+
* <p>
1427+
* Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization.
1428+
* <p>
14121429
* The {@code objectToSave} must not be collection-like.
14131430
*
14141431
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
@@ -1420,6 +1437,11 @@ default long exactCount(Query query, String collectionName) {
14201437

14211438
/**
14221439
* Insert a Collection of objects into a collection in a single batch write to the database.
1440+
* <p>
1441+
* If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set
1442+
* with the generated Id from MongoDB.
1443+
* <p>
1444+
* Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization.
14231445
*
14241446
* @param batchToSave the batch of objects to save. Must not be {@literal null}.
14251447
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
@@ -1431,6 +1453,11 @@ default long exactCount(Query query, String collectionName) {
14311453

14321454
/**
14331455
* Insert a batch of objects into the specified collection in a single batch write to the database.
1456+
* <p>
1457+
* If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set
1458+
* with the generated Id from MongoDB.
1459+
* <p>
1460+
* Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization.
14341461
*
14351462
* @param batchToSave the list of objects to save. Must not be {@literal null}.
14361463
* @param collectionName name of the collection to store the object in. Must not be {@literal null}.
@@ -1441,6 +1468,11 @@ default long exactCount(Query query, String collectionName) {
14411468
/**
14421469
* Insert a mixed Collection of objects into a database collection determining the collection name to use based on the
14431470
* class.
1471+
* <p>
1472+
* If an object within the batch has an {@literal Id} property which holds a {@literal null} value, it will be set
1473+
* with the generated Id from MongoDB.
1474+
* <p>
1475+
* Inserting new objects will trigger {@link org.springframework.data.annotation.Version} property initialization.
14441476
*
14451477
* @param objectsToSave the list of objects to save. Must not be {@literal null}.
14461478
* @return the inserted objects.
@@ -1458,14 +1490,20 @@ default long exactCount(Query query, String collectionName) {
14581490
* String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
14591491
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
14601492
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
1461-
* Type Conversion"</a> for more details. <br />
1493+
* Type Conversion"</a> for more details.
1494+
* <p>
1495+
* A potential {@link org.springframework.data.annotation.Version} the property will be auto incremented. The
1496+
* operation raises an error in case the document has been modified in between.
1497+
* <p>
14621498
* The {@code objectToSave} must not be collection-like.
14631499
*
14641500
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
14651501
* @return the saved object.
14661502
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
14671503
* @throws org.springframework.data.mapping.MappingException if the target collection name cannot be
14681504
* {@link #getCollectionName(Class) derived} from the given object type.
1505+
* @throws org.springframework.dao.OptimisticLockingFailureException in case of version mismatch in case a
1506+
* {@link org.springframework.data.annotation.Version} is defined.
14691507
*/
14701508
<T> T save(T objectToSave);
14711509

@@ -1478,19 +1516,29 @@ default long exactCount(Query query, String collectionName) {
14781516
* String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
14791517
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
14801518
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation">Spring's Type
1481-
* Conversion</a> for more details. <br />
1519+
* Conversion</a> for more details.
1520+
* <p>
1521+
* A potential {@link org.springframework.data.annotation.Version} the property will be auto incremented. The
1522+
* operation raises an error in case the document has been modified in between.
1523+
* <p>
14821524
* The {@code objectToSave} must not be collection-like.
14831525
*
14841526
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
14851527
* @param collectionName name of the collection to store the object in. Must not be {@literal null}.
14861528
* @return the saved object.
14871529
* @throws IllegalArgumentException in case the {@code objectToSave} is collection-like.
1530+
* @throws org.springframework.dao.OptimisticLockingFailureException in case of version mismatch in case a
1531+
* {@link org.springframework.data.annotation.Version} is defined.
14881532
*/
14891533
<T> T save(T objectToSave, String collectionName);
14901534

14911535
/**
14921536
* Performs an upsert. If no document is found that matches the query, a new document is created and inserted by
1493-
* combining the query document and the update document. <br />
1537+
* combining the query document and the update document.
1538+
* <p>
1539+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
1540+
* incremented if not explicitly specified in the update.
1541+
* <p>
14941542
* <strong>NOTE:</strong> {@link Query#getSortObject() sorting} is not supported by {@code db.collection.updateOne}.
14951543
* Use {@link #findAndModify(Query, UpdateDefinition, FindAndModifyOptions, Class, String)} instead.
14961544
*
@@ -1532,6 +1580,9 @@ default long exactCount(Query query, String collectionName) {
15321580
/**
15331581
* Performs an upsert. If no document is found that matches the query, a new document is created and inserted by
15341582
* combining the query document and the update document.
1583+
* <p>
1584+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
1585+
* incremented if not explicitly specified in the update.
15351586
*
15361587
* @param query the query document that specifies the criteria used to select a document to be upserted. Must not be
15371588
* {@literal null}.
@@ -1549,6 +1600,9 @@ default long exactCount(Query query, String collectionName) {
15491600
/**
15501601
* Updates the first object that is found in the collection of the entity class that matches the query document with
15511602
* the provided update document.
1603+
* <p>
1604+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
1605+
* incremented if not explicitly specified in the update.
15521606
*
15531607
* @param query the query document that specifies the criteria used to select a document to be updated. Must not be
15541608
* {@literal null}.
@@ -1587,7 +1641,10 @@ default long exactCount(Query query, String collectionName) {
15871641

15881642
/**
15891643
* Updates the first object that is found in the specified collection that matches the query document criteria with
1590-
* the provided updated document. <br />
1644+
* the provided updated document.
1645+
* <p>
1646+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
1647+
* incremented if not explicitly specified in the update.
15911648
*
15921649
* @param query the query document that specifies the criteria used to select a document to be updated. Must not be
15931650
* {@literal null}.
@@ -1605,6 +1662,9 @@ default long exactCount(Query query, String collectionName) {
16051662
/**
16061663
* Updates all objects that are found in the collection for the entity class that matches the query document criteria
16071664
* with the provided updated document.
1665+
* <p>
1666+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
1667+
* incremented if not explicitly specified in the update.
16081668
*
16091669
* @param query the query document that specifies the criteria used to select a document to be updated. Must not be
16101670
* {@literal null}.
@@ -1642,6 +1702,9 @@ default long exactCount(Query query, String collectionName) {
16421702
/**
16431703
* Updates all objects that are found in the collection for the entity class that matches the query document criteria
16441704
* with the provided updated document.
1705+
* <p>
1706+
* A potential {@link org.springframework.data.annotation.Version} property of the {@literal entityClass} will be auto
1707+
* incremented if not explicitly specified in the update.
16451708
*
16461709
* @param query the query document that specifies the criteria used to select a document to be updated. Must not be
16471710
* {@literal null}.

0 commit comments

Comments
 (0)