How it works...

This recipe illustrated how Spring 5 supports MongoDB when it comes to both typical and Reactive Spring Data JPA. If an application needs non-blocking data repository transactions, the typical spring-boot-starter-data-mongodb starter POM dependency needs to be added in pom.xml. If the application is more of the resilient type, then the spring-boot-starter-data-mongodb-reactive starter must be of the required dependency. This kind of support is also true for Apache Cassandra and Redis.

Reactive Spring Data MongoDB is a module of Spring Data that provides support starting from entity models down to MongoDB reactive repositories, providing data from derived queries wrapped in a Flux<T> or Mono<T> stream. The module has both ReactiveMongoRepository and ReactiveCrudRepository, which can be used synonymously without any difference in performance, since the former is derived from the latter. The only matter here is the use of ReactiveMongoTemplate, which can make reference to Mongo databases with the help of ReactiveMongoRepository but not with ReactiveCrudRepository.

Since MongoDB is a document-based NoSQL database, it requires its entity model to be annotated with @Document to hold all the binary JSON (BSON) data to be persisted into its collections. The idea of Collections depicts the concept of a relation containing all Mongo unstructured documents. MongoDB is an unstructured database, so it has no concept of relational models such as databases and table schema.

The @Id annotation is associated with the primary key of the document and is mapped to the Object _id of Collections by default. To retrieve the _idper Document, include it inside the entity model as a BigInteger object, not to be confused with Longid, which is the custom data assigned per Department record.

To successfully retrieve data from Collections, each entity model must have a constructor with all the injected fields as parameters and a @PersistenceConstructor annotation mapped to it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.106.7