You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mapping.adoc
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -211,6 +211,7 @@ The MappingMongoConverter can use metadata to drive the mapping of objects to do
211
211
* `@PersistenceConstructor` - marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved DBObject.
212
212
* `@Value` - this annotation is part of the Spring Framework . Within the mapping framework it can be applied to constructor arguments. This lets you use a Spring Expression Language statement to transform a key's value retrieved in the database before it is used to construct a domain object. In order to reference a property of a given document one has to use expressions like: `@Value("#root.myProperty")` where `root` refers to the root of the given document.
213
213
* `@Field` - applied at the field level and described the name of the field as it will be represented in the MongoDB BSON document thus allowing the name to be different than the fieldname of the class.
214
+
* `@Version` - applied at field level is used for optimistic locking and checked for modification on save operations. The initial value is `zero` which is bumped automatically on every update.
214
215
215
216
The mapping metadata infrastructure is defined in a seperate spring-data-commons project that is technology agnostic. Specific subclasses are using in the MongoDB support to support annotation based metadata. Other strategies are also possible to put in place if there is demand.
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mongodb.adoc
+34Lines changed: 34 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -938,6 +938,40 @@ You can use several overloaded methods to remove an object from the database.
938
938
939
939
* *remove* Remove the given document based on one of the following: a specific object instance, a query document criteria combined with a class or a query document criteria combined with a specific collection name.
940
940
941
+
[[mongo-template.optimistic-locking]]
942
+
=== Optimistic locking
943
+
944
+
The `@Version` annotation provides a JPA similar semantic in the context of MongoDB and makes sure updates are only applied to documents with matching version. Therefore the actual value of the version property is added to the update query in a way that the update won't have any effect if another operation altered the document in between. In that case an `OptimisticLockingFailureException` is thrown.
945
+
946
+
====
947
+
[source,java]
948
+
----
949
+
@Document
950
+
class Person {
951
+
952
+
@Id String id;
953
+
String firstname;
954
+
String lastname;
955
+
@Version Long version;
956
+
}
957
+
958
+
Person daenerys = template.insert(new Person("Daenerys")); <1>
959
+
960
+
Person tmp = teplate.findOne(query(where("id").is(daenerys.getId())), Person.class); <2>
<1> Intially insert document. `version` is set to `0`.
968
+
<2> Load the just inserted document `version` is still `0`.
969
+
<3> Update document with `version = 0`. Set the `lastname` and bump `version` to `1`.
970
+
<4> Try to update previously loaded document sill having `version = 0` fails with `OptimisticLockingFailureException` as the current `version` is `1`.
971
+
====
972
+
973
+
IMPORTANT: Using MongoDB driver version 3 requires to set the `WriteConcern` to `ACKNOWLEDGED`. Otherwise `OptimisticLockingFailureException` can be silently swallowed.
0 commit comments