Browse Source

Polishing.

Move examples into test source root to ensure proper compilation. Remove lingering asciidoc resources.
Merge entity callbacks into lifecycle events. Add links from MongoDB overview.
labs/antora
Mark Paluch 2 years ago
parent
commit
161ec9aacb
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 4
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/example/MongoApplication.java
  2. 0
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/example/Person.java
  3. 1
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/example/ReactiveMongoApplication.java
  4. BIN
      src/main/antora/modules/ROOT/assets/images/epub-cover.png
  5. 12
      src/main/antora/modules/ROOT/assets/images/epub-cover.svg
  6. 10
      src/main/antora/modules/ROOT/nav.adoc
  7. 1
      src/main/antora/modules/ROOT/pages/kotlin/object-mapping.adoc
  8. 23
      src/main/antora/modules/ROOT/pages/mongodb.adoc
  9. 6
      src/main/antora/modules/ROOT/pages/mongodb/getting-started.adoc
  10. 52
      src/main/antora/modules/ROOT/pages/mongodb/lifecycle-events.adoc
  11. 45
      src/main/antora/modules/ROOT/pages/mongodb/mapping/entity-callbacks.adoc
  12. 18
      src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc
  13. 6
      src/main/antora/modules/ROOT/pages/mongodb/template-config.adoc
  14. 4
      src/main/antora/modules/ROOT/pages/mongodb/template-crud-operations.adoc
  15. 2
      src/main/antora/modules/ROOT/pages/repositories/core-concepts.adoc
  16. 2
      src/main/antora/modules/ROOT/pages/repositories/projections.adoc
  17. BIN
      src/main/asciidoc/images/epub-cover.png
  18. 12
      src/main/asciidoc/images/epub-cover.svg
  19. BIN
      src/main/asciidoc/images/jconsole.png

4
src/main/antora/modules/ROOT/examples/MongoApplication.java → spring-data-mongodb/src/test/java/org/springframework/data/mongodb/example/MongoApplication.java

@ -16,12 +16,10 @@ @@ -16,12 +16,10 @@
// tag::file[]
package org.springframework.data.mongodb.example;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import com.mongodb.client.MongoClients;

0
src/main/antora/modules/ROOT/examples/Person.java → spring-data-mongodb/src/test/java/org/springframework/data/mongodb/example/Person.java

1
src/main/antora/modules/ROOT/examples/ReactiveMongoApplication.java → spring-data-mongodb/src/test/java/org/springframework/data/mongodb/example/ReactiveMongoApplication.java

@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
package org.springframework.data.mongodb.example;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;

BIN
src/main/antora/modules/ROOT/assets/images/epub-cover.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

12
src/main/antora/modules/ROOT/assets/images/epub-cover.svg

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 8.9 KiB

10
src/main/antora/modules/ROOT/nav.adoc

@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
* xref:mongodb.adoc[]
** xref:mongodb/getting-started.adoc[]
** xref:mongodb/configuration.adoc[]
** xref:mongodb/template-api.adoc[]
*** xref:mongodb/template-config.adoc[]
*** xref:mongodb/template-crud-operations.adoc[]
@ -16,21 +17,22 @@ @@ -16,21 +17,22 @@
*** xref:mongodb/aggregation-framework.adoc[]
*** xref:mongodb/template-collection-management.adoc[]
**** xref:mongodb/template-collection-schema.adoc[]
*** xref:mongodb/template-gridfs.adoc[]
** xref:mongodb/template-gridfs.adoc[]
** xref:mongodb/mapping/mapping.adoc[]
*** xref:mongodb/mapping/custom-conversions.adoc[Type based Converter]
*** xref:mongodb/mapping/property-converters.adoc[]
*** xref:mongodb/mapping/unwrapping-entities.adoc[]
*** xref:mongodb/mapping/document-references.adoc[Object References]
*** xref:mongodb/mapping/entity-callbacks.adoc[Entity Callbacks]
*** xref:mongodb/mapping/lifecycle-events.adoc[]
*** xref:mongodb/mapping/mapping-index-management.adoc[]
** xref:mongodb/lifecycle-events.adoc[]
** xref:mongodb/auditing.adoc[]
** xref:mongodb/client-session-transactions.adoc[]
** xref:mongodb/change-streams.adoc[]
** xref:mongodb/tailable-cursors.adoc[]
** xref:mongodb/sharding.adoc[]
** xref:mongodb/mongo-encryption.adoc[]
** xref:mongodb/auditing.adoc[]
// Repository
* xref:repositories.adoc[]

1
src/main/antora/modules/ROOT/pages/kotlin/object-mapping.adoc

@ -1 +0,0 @@ @@ -1 +0,0 @@
// include::{commons}@data-commons::page$kotlin/object-mapping.adoc[]

23
src/main/antora/modules/ROOT/pages/mongodb.adoc

@ -4,17 +4,18 @@ @@ -4,17 +4,18 @@
Spring Data support for MongoDB contains a wide range of features:
* Spring configuration support with Java-based `@Configuration` classes or an XML namespace for a Mongo driver instance and replica sets.
* `MongoTemplate` helper class that increases productivity when performing common Mongo operations. Includes integrated object mapping between documents and POJOs.
* Exception translation into Spring's portable Data Access Exception hierarchy.
* Feature-rich Object Mapping integrated with Spring's Conversion Service.
* Annotation-based mapping metadata that is extensible to support other metadata formats.
* Persistence and mapping lifecycle events.
* Java-based Query, Criteria, and Update DSLs.
* Automatic implementation of Repository interfaces, including support for custom finder methods.
* QueryDSL integration to support type-safe queries.
* Multi Document Transactions.
* GeoSpatial integration.
* xref:mongodb/template-config.adoc[Spring configuration support] with Java-based `@Configuration` classes or an XML namespace for a Mongo driver instance and replica sets.
* xref:mongodb/template-api.adoc[`MongoTemplate` helper class] that increases productivity when performing common Mongo operations.
Includes integrated object mapping between documents and POJOs.
* xref:mongodb/template-api.adoc#mongo-template.exception-translation[Exception translation] into Spring's portable Data Access Exception hierarchy.
* Feature-rich xref:mongodb/mapping/mapping.adoc[Object Mapping] integrated with Spring's Conversion Service.
* xref:mongodb/mapping/mapping.adoc#mapping-usage-annotations[Annotation-based mapping metadata] that is extensible to support other metadata formats.
* xref:mongodb/lifecycle-events.adoc[Persistence and mapping lifecycle events].
* xref:mongodb/template-query-options.adoc[Java-based Query, Criteria, and Update DSLs].
* Automatic implementation of xref:repositories.adoc[Repository interfaces], including support for custom query methods.
* xref:mongodb/repositories/repositories.adoc#mongodb.repositories.queries.type-safe[QueryDSL integration] to support type-safe queries.
* xref:mongodb/client-session-transactions.adoc[Multi-Document Transactions].
* xref:mongodb/template-query-operations.adoc#mongo.geo-json[GeoSpatial integration].
For most tasks, you should use `MongoTemplate` or the Repository support, which both leverage the rich mapping functionality.
`MongoTemplate` is the place to look for accessing functionality such as incrementing counters or ad-hoc CRUD operations.

6
src/main/antora/modules/ROOT/pages/mongodb/getting-started.adoc

@ -19,7 +19,7 @@ Then you can create a `Person` class to persist: @@ -19,7 +19,7 @@ Then you can create a `Person` class to persist:
====
[source,java]
----
include::example$Person.java[tags=file]
include::example$example/Person.java[tags=file]
----
====
@ -31,14 +31,14 @@ Imperative:: @@ -31,14 +31,14 @@ Imperative::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
include::example$MongoApplication.java[tags=file]
include::example$example/MongoApplication.java[tags=file]
----
Reactive::
+
[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
----
include::example$ReactiveMongoApplication.java[tags=file]
include::example$example/ReactiveMongoApplication.java[tags=file]
----
======

52
src/main/antora/modules/ROOT/pages/mongodb/mapping/lifecycle-events.adoc → src/main/antora/modules/ROOT/pages/mongodb/lifecycle-events.adoc

@ -49,8 +49,58 @@ Declaring these beans in your Spring ApplicationContext causes them to be invoke @@ -49,8 +49,58 @@ Declaring these beans in your Spring ApplicationContext causes them to be invoke
* `onAfterConvert`: Called in `MongoTemplate` `find`, `findAndRemove`, `findOne`, and `getCollection` methods after the `Document` has been retrieved from the database was converted to a POJO.
====
NOTE: Lifecycle events are only emitted for root level types. Complex types used as properties within a document root are not subject to event publication unless they are document references annotated with `@DBRef`.
NOTE: Lifecycle events are only emitted for root level types.
Complex types used as properties within a document root are not subject to event publication unless they are document references annotated with `@DBRef`.
WARNING: Lifecycle events depend on an `ApplicationEventMulticaster`, which in case of the `SimpleApplicationEventMulticaster` can be configured with a `TaskExecutor`, and therefore gives no guarantees when an Event is processed.
include::{commons}@data-commons::page$entity-callbacks.adoc[leveloffset=+1]
[[mongo.entity-callbacks]]
== Store specific EntityCallbacks
Spring Data MongoDB uses the `EntityCallback` API for its auditing support and reacts on the following callbacks.
.Supported Entity Callbacks
[%header,cols="4"]
|===
| Callback
| Method
| Description
| Order
| `ReactiveBeforeConvertCallback`
`BeforeConvertCallback`
| `onBeforeConvert(T entity, String collection)`
| Invoked before a domain object is converted to `org.bson.Document`.
| `Ordered.LOWEST_PRECEDENCE`
| `ReactiveAfterConvertCallback`
`AfterConvertCallback`
| `onAfterConvert(T entity, org.bson.Document target, String collection)`
| Invoked after a domain object is loaded. +
Can modify the domain object after reading it from a `org.bson.Document`.
| `Ordered.LOWEST_PRECEDENCE`
| `ReactiveAuditingEntityCallback`
`AuditingEntityCallback`
| `onBeforeConvert(Object entity, String collection)`
| Marks an auditable entity _created_ or _modified_
| 100
| `ReactiveBeforeSaveCallback`
`BeforeSaveCallback`
| `onBeforeSave(T entity, org.bson.Document target, String collection)`
| Invoked before a domain object is saved. +
Can modify the target, to be persisted, `Document` containing all mapped entity information.
| `Ordered.LOWEST_PRECEDENCE`
| `ReactiveAfterSaveCallback`
`AfterSaveCallback`
| `onAfterSave(T entity, org.bson.Document target, String collection)`
| Invoked before a domain object is saved. +
Can modify the domain object, to be returned after save, `Document` containing all mapped entity information.
| `Ordered.LOWEST_PRECEDENCE`
|===

45
src/main/antora/modules/ROOT/pages/mongodb/mapping/entity-callbacks.adoc

@ -1,45 +0,0 @@ @@ -1,45 +0,0 @@
include::{commons}@data-commons::page$entity-callbacks.adoc[]
[[mongo.entity-callbacks]]
== Store specific EntityCallbacks
Spring Data MongoDB uses the `EntityCallback` API for its auditing support and reacts on the following callbacks.
.Supported Entity Callbacks
[%header,cols="4"]
|===
| Callback
| Method
| Description
| Order
| Reactive/BeforeConvertCallback
| `onBeforeConvert(T entity, String collection)`
| Invoked before a domain object is converted to `org.bson.Document`.
| `Ordered.LOWEST_PRECEDENCE`
| Reactive/AfterConvertCallback
| `onAfterConvert(T entity, org.bson.Document target, String collection)`
| Invoked after a domain object is loaded. +
Can modify the domain object after reading it from a `org.bson.Document`.
| `Ordered.LOWEST_PRECEDENCE`
| Reactive/AuditingEntityCallback
| `onBeforeConvert(Object entity, String collection)`
| Marks an auditable entity _created_ or _modified_
| 100
| Reactive/BeforeSaveCallback
| `onBeforeSave(T entity, org.bson.Document target, String collection)`
| Invoked before a domain object is saved. +
Can modify the target, to be persisted, `Document` containing all mapped entity information.
| `Ordered.LOWEST_PRECEDENCE`
| Reactive/AfterSaveCallback
| `onAfterSave(T entity, org.bson.Document target, String collection)`
| Invoked before a domain object is saved. +
Can modify the domain object, to be returned after save, `Document` containing all mapped entity information.
| `Ordered.LOWEST_PRECEDENCE`
|===

18
src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc

@ -9,21 +9,26 @@ The `MappingMongoConverter` also lets you map objects to documents without provi @@ -9,21 +9,26 @@ The `MappingMongoConverter` also lets you map objects to documents without provi
This section describes the features of the `MappingMongoConverter`, including fundamentals, how to use conventions for mapping objects to documents and how to override those conventions with annotation-based mapping metadata.
include::{commons}@data-commons::page$object-mapping.adoc[leveloffset=+1]
[[mapping-conventions]]
== Convention-based Mapping
`MappingMongoConverter` has a few conventions for mapping objects to documents when no additional mapping metadata is provided. The conventions are:
`MappingMongoConverter` has a few conventions for mapping objects to documents when no additional mapping metadata is provided.
The conventions are:
* The short Java class name is mapped to the collection name in the following manner. The class `com.bigbank.SavingsAccount` maps to the `savingsAccount` collection name.
* The short Java class name is mapped to the collection name in the following manner.
The class `com.bigbank.SavingsAccount` maps to the `savingsAccount` collection name.
* All nested objects are stored as nested objects in the document and *not* as DBRefs.
* The converter uses any Spring Converters registered with it to override the default mapping of object properties to document fields and values.
* The fields of an object are used to convert to and from fields in the document. Public `JavaBean` properties are not used.
* If you have a single non-zero-argument constructor whose constructor argument names match top-level field names of document, that constructor is used. Otherwise, the zero-argument constructor is used. If there is more than one non-zero-argument constructor, an exception will be thrown.
* The fields of an object are used to convert to and from fields in the document.
Public `JavaBean` properties are not used.
* If you have a single non-zero-argument constructor whose constructor argument names match top-level field names of document, that constructor is used.Otherwise, the zero-argument constructor is used.If there is more than one non-zero-argument constructor, an exception will be thrown.
[[mapping.conventions.id-field]]
=== How the `_id` field is handled in the mapping layer.
MongoDB requires that you have an `_id` field for all documents. If you don't provide one the driver will assign a ObjectId with a generated value. The "_id" field can be of any type the, other than arrays, so long as it is unique. The driver naturally supports all primitive types and Dates. When using the `MappingMongoConverter` there are certain rules that govern how properties from the Java class is mapped to this `_id` field.
MongoDB requires that you have an `_id` field for all documents.If you don't provide one the driver will assign a ObjectId with a generated value.The "_id" field can be of any type the, other than arrays, so long as it is unique.The driver naturally supports all primitive types and Dates.When using the `MappingMongoConverter` there are certain rules that govern how properties from the Java class is mapped to this `_id` field.
The following outlines what field will be mapped to the `_id` document field:
@ -569,6 +574,7 @@ Additional examples for using the `@PersistenceConstructor` annotation can be fo @@ -569,6 +574,7 @@ Additional examples for using the `@PersistenceConstructor` annotation can be fo
[[mapping-usage-events]]
=== Mapping Framework Events
Events are fired throughout the lifecycle of the mapping process. This is described in the xref:mongodb/mapping/lifecycle-events.adoc[Lifecycle Events] section.
Events are fired throughout the lifecycle of the mapping process.
This is described in the xref:mongodb/lifecycle-events.adoc[Lifecycle Events] section.
Declaring these beans in your Spring ApplicationContext causes them to be invoked whenever the event is dispatched.

6
src/main/antora/modules/ROOT/pages/mongodb/template-config.adoc

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
[[mongo-template.instantiating]]
= MongoTemplate Configuration
= Configuration
You can use the following configuration to create and register an instance of `MongoTemplate`, as the following example shows:
@ -115,7 +115,7 @@ public class MyAppWriteConcernResolver implements WriteConcernResolver { @@ -115,7 +115,7 @@ public class MyAppWriteConcernResolver implements WriteConcernResolver {
[[mongo-template.entity-lifecycle-events]]
== Publish entity lifecycle events
The template publishes xref:mongodb/mapping/lifecycle-events.adoc#mongodb.mapping-usage.events[lifecycle events].
The template publishes xref:mongodb/lifecycle-events.adoc#mongodb.mapping-usage.events[lifecycle events].
In case there are no listeners present, this feature can be disabled.
[source,java]
@ -131,7 +131,7 @@ MongoOperations mongoTemplate(MongoClient mongoClient) { @@ -131,7 +131,7 @@ MongoOperations mongoTemplate(MongoClient mongoClient) {
[[mongo-template.entity-callbacks-config]]
== Configure EntityCallbacks
Nest to lifecycle events the template invokes xref:mongodb/mapping/entity-callbacks.adoc[EntityCallbacks] which can be (if not auto configured) set via the template API.
Nest to lifecycle events the template invokes xref:mongodb/lifecycle-events.adoc#mongo.entity-callbacks[EntityCallbacks] which can be (if not auto configured) set via the template API.
[tabs]
======

4
src/main/antora/modules/ROOT/pages/mongodb/template-crud-operations.adoc

@ -9,7 +9,7 @@ Consider the following class: @@ -9,7 +9,7 @@ Consider the following class:
[source,java]
----
include::example$Person.java[tags=class]
include::example$example/Person.java[tags=class]
----
Given the `Person` class in the preceding example, you can save, update and delete the object, as the following example shows:
@ -246,7 +246,7 @@ Mono<BulkWriteResult> result = template.bulkOps(BulkMode.ORDERED, Person.class) @@ -246,7 +246,7 @@ Mono<BulkWriteResult> result = template.bulkOps(BulkMode.ORDERED, Person.class)
[NOTE]
====
Server performance of batch and bulk is identical.
However bulk operations do not publish xref:mongodb/mapping/lifecycle-events.adoc[lifecycle events].
However bulk operations do not publish xref:mongodb/lifecycle-events.adoc[lifecycle events].
====
[[mongodb-template-update]]

2
src/main/antora/modules/ROOT/pages/repositories/core-concepts.adoc

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
include::{commons}@data-commons::page$repositories/core-concepts.adoc[]
[[cassandra.entity-persistence.state-detection-strategies]]
[[mongodb.entity-persistence.state-detection-strategies]]
include::{commons}@data-commons::page$is-new-state-detection.adoc[leveloffset=+1]
[NOTE]

2
src/main/antora/modules/ROOT/pages/repositories/projections.adoc

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
[[cassandra.projections]]
[[mongodb.projections]]
= Projections
include::{commons}@data-commons::page$repositories/projections.adoc[leveloffset=+1]

BIN
src/main/asciidoc/images/epub-cover.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

12
src/main/asciidoc/images/epub-cover.svg

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 8.9 KiB

BIN
src/main/asciidoc/images/jconsole.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

Loading…
Cancel
Save