From cfaf12234501122aa5e1b42432bba86ee404a368 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 21 Aug 2015 11:02:21 +0200 Subject: [PATCH 1/3] DATAMONGO-1275 - Update Reference Documentation. Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-cross-store/pom.xml | 4 ++-- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb-log4j/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 74b7a38a74..6b01e9d912 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.8.0.BUILD-SNAPSHOT + 1.8.0.DATAMONGO-1275-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index 7d18f752fc..91b228de03 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 1.8.0.BUILD-SNAPSHOT + 1.8.0.DATAMONGO-1275-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 1.8.0.BUILD-SNAPSHOT + 1.8.0.DATAMONGO-1275-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 13c0985a6a..2b7251d5d9 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 1.8.0.BUILD-SNAPSHOT + 1.8.0.DATAMONGO-1275-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index 64d6f7864a..ef9da4358a 100644 --- a/spring-data-mongodb-log4j/pom.xml +++ b/spring-data-mongodb-log4j/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.8.0.BUILD-SNAPSHOT + 1.8.0.DATAMONGO-1275-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 819112059b..90649b0435 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 1.8.0.BUILD-SNAPSHOT + 1.8.0.DATAMONGO-1275-SNAPSHOT ../pom.xml From 4842e9969e79729e048620a6bc944ec23f78a016 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 21 Aug 2015 11:03:46 +0200 Subject: [PATCH 2/3] DATAMONGO-1275 - Add documentation for Optimistic Locking. --- src/main/asciidoc/reference/mapping.adoc | 1 + src/main/asciidoc/reference/mongodb.adoc | 34 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/main/asciidoc/reference/mapping.adoc b/src/main/asciidoc/reference/mapping.adoc index 8da6cf15b4..087fdf890b 100644 --- a/src/main/asciidoc/reference/mapping.adoc +++ b/src/main/asciidoc/reference/mapping.adoc @@ -211,6 +211,7 @@ The MappingMongoConverter can use metadata to drive the mapping of objects to do * `@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. * `@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. * `@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. +* `@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. 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. diff --git a/src/main/asciidoc/reference/mongodb.adoc b/src/main/asciidoc/reference/mongodb.adoc index a7a8c3c89b..6fd98ab96b 100644 --- a/src/main/asciidoc/reference/mongodb.adoc +++ b/src/main/asciidoc/reference/mongodb.adoc @@ -938,6 +938,40 @@ You can use several overloaded methods to remove an object from the database. * *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. +[[mongo-template.optimistic-locking]] +=== Optimistic locking + +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. + +==== +[source,java] +---- +@Document +class Person { + + @Id String id; + String firstname; + String lastname; + @Version Long version; +} + +Person daenerys = template.insert(new Person("Daenerys")); <1> + +Person tmp = teplate.findOne(query(where("id").is(daenerys.getId())), Person.class); <2> + +daenerys.setLastname("Targaryen"); +template.save(daenerys); <3> + +template.save(tmp); // throws OptimisticLockingFailureException <4> +---- +<1> Intially insert document. `version` is set to `0`. +<2> Load the just inserted document `version` is still `0`. +<3> Update document with `version = 0`. Set the `lastname` and bump `version` to `1`. +<4> Try to update previously loaded document sill having `version = 0` fails with `OptimisticLockingFailureException` as the current `version` is `1`. +==== + +IMPORTANT: Using MongoDB driver version 3 requires to set the `WriteConcern` to `ACKNOWLEDGED`. Otherwise `OptimisticLockingFailureException` can be silently swallowed. + [[mongo.query]] == Querying Documents From d659d5d2e9f432b1a0cdeaba3f9e605a6a560e61 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 21 Aug 2015 11:05:14 +0200 Subject: [PATCH 3/3] DATAMONGO-1275 - Fix broken links in reference documentation. --- src/main/asciidoc/reference/mongo-repositories.adoc | 2 +- src/main/asciidoc/reference/mongodb.adoc | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/asciidoc/reference/mongo-repositories.adoc b/src/main/asciidoc/reference/mongo-repositories.adoc index 5ee9f88870..5b949be208 100644 --- a/src/main/asciidoc/reference/mongo-repositories.adoc +++ b/src/main/asciidoc/reference/mongo-repositories.adoc @@ -381,7 +381,7 @@ MongoDB repository support integrates with the http://www.querydsl.com/[QueryDSL * Adopts better to refactoring changes in domain types * Incremental query definition is easier -Please refer to the QueryDSL documentation which describes how to bootstrap your environment for APT based code generation http://source.mysema.com/static/querydsl/2.1.2/reference/html/ch02.html#d0e112[using Maven] or http://source.mysema.com/static/querydsl/2.1.2/reference/html/ch02.html#d0e131[using Ant]. +Please refer to the http://www.querydsl.com/static/querydsl/latest/reference/html/[QueryDSL documentation] which describes how to bootstrap your environment for APT based code generation using Maven or Ant. Using QueryDSL you will be able to write queries as shown below diff --git a/src/main/asciidoc/reference/mongodb.adoc b/src/main/asciidoc/reference/mongodb.adoc index 6fd98ab96b..976f9c18a5 100644 --- a/src/main/asciidoc/reference/mongodb.adoc +++ b/src/main/asciidoc/reference/mongodb.adoc @@ -64,7 +64,7 @@ You will also need to add the location of the Spring Milestone repository for ma ---- -The repository is also http://shrub.appspot.com/maven.springframework.org/milestone/org/springframework/data/[browseable here]. +The repository is also http://repo.spring.io/milestone/org/springframework/data/[browseable here]. You may also want to set the logging level to `DEBUG` to see some additional information, edit the log4j.properties file to have @@ -1193,13 +1193,13 @@ The geo near operations return a `GeoResults` wrapper object that encapsulates ` [[mongo.geo-json]] === GeoJSON Support -MongoDB supports http://geojeson.org/[GeoJSON] and simple (legacy) coordinate pairs for geospatial data. Those formats can both be used for storing as well as querying data. +MongoDB supports http://geojson.org/[GeoJSON] and simple (legacy) coordinate pairs for geospatial data. Those formats can both be used for storing as well as querying data. NOTE: Please refer to the http://docs.mongodb.org/manual/core/2dsphere/#geospatial-indexes-store-geojson/[MongoDB manual on GeoJSON support] to learn about requirements and restrictions. ==== GeoJSON types in domain classes -Usage of http://geojeson.org/[GeoJSON] types in domain classes is straight forward. The `org.springframework.data.mongodb.core.geo` package contains types like `GeoJsonPoint`, `GeoJsonPolygon` and others. Those are extensions to the existing `org.springframework.data.geo` types. +Usage of http://geojson.org/[GeoJSON] types in domain classes is straight forward. The `org.springframework.data.mongodb.core.geo` package contains types like `GeoJsonPoint`, `GeoJsonPolygon` and others. Those are extensions to the existing `org.springframework.data.geo` types. ==== [source,java]