Browse Source
Related to: DATAMONGO-365, DATAMONGO-384, DATAMONGO-2192, DATAMONGO-2407 Original pull request: #853.pull/862/head
7 changed files with 124 additions and 340 deletions
@ -1,260 +0,0 @@
@@ -1,260 +0,0 @@
|
||||
[[mongo.cross.store]] |
||||
= Cross Store Support |
||||
|
||||
WARNING: This feature has been deprecated and will be removed without replacement. |
||||
|
||||
Sometimes you need to store data in multiple data stores, and these data stores need to be of different types. One might be relational while the other is a document store. For this use case, we created a separate module in the MongoDB support that handles what we call "`cross-store support`". The current implementation is based on JPA as the driver for the relational database and we let select fields in the Entities be stored in a Mongo database. In addition to letting you store your data in two stores, we also coordinate persistence operations for the non-transactional MongoDB store with the transaction life-cycle for the relational database. |
||||
|
||||
[[mongodb_cross-store-configuration]] |
||||
== Cross Store Configuration |
||||
|
||||
Assuming that you have a working JPA application and would like to add some cross-store persistence for MongoDB, what do you have to add to your configuration? |
||||
|
||||
First, you need to add a dependency on the cross-store module. If you use Maven, you can add the following dependency to your pom: |
||||
|
||||
.Example Maven pom.xml with `spring-data-mongodb-cross-store` dependency |
||||
==== |
||||
[source,xml] |
||||
---- |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
... |
||||
|
||||
<!-- Spring Data --> |
||||
<dependency> |
||||
<groupId>org.springframework.data</groupId> |
||||
<artifactId>spring-data-mongodb-cross-store</artifactId> |
||||
<version>${spring.data.mongo.version}</version> |
||||
</dependency> |
||||
|
||||
... |
||||
|
||||
</project> |
||||
---- |
||||
==== |
||||
|
||||
Once you have added the dependency, you need to enable AspectJ for the project. The cross-store support is implemented with AspectJ aspects so, if you enable compile-time AspectJ support, the cross-store features become available to your project. In Maven, you would add an additional plugin to the `<build>` section of the pom, as follows: |
||||
|
||||
.Example Maven pom.xml with AspectJ plugin enabled |
||||
==== |
||||
[source,xml] |
||||
---- |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
... |
||||
|
||||
<build> |
||||
<plugins> |
||||
|
||||
… |
||||
|
||||
<plugin> |
||||
<groupId>org.codehaus.mojo</groupId> |
||||
<artifactId>aspectj-maven-plugin</artifactId> |
||||
<version>1.0</version> |
||||
<dependencies> |
||||
<!-- NB: You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) --> |
||||
<dependency> |
||||
<groupId>org.aspectj</groupId> |
||||
<artifactId>aspectjrt</artifactId> |
||||
<version>${aspectj.version}</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.aspectj</groupId> |
||||
<artifactId>aspectjtools</artifactId> |
||||
<version>${aspectj.version}</version> |
||||
</dependency> |
||||
</dependencies> |
||||
<executions> |
||||
<execution> |
||||
<goals> |
||||
<goal>compile</goal> |
||||
<goal>test-compile</goal> |
||||
</goals> |
||||
</execution> |
||||
</executions> |
||||
<configuration> |
||||
<outxml>true</outxml> |
||||
<aspectLibraries> |
||||
<aspectLibrary> |
||||
<groupId>org.springframework</groupId> |
||||
<artifactId>spring-aspects</artifactId> |
||||
</aspectLibrary> |
||||
<aspectLibrary> |
||||
<groupId>org.springframework.data</groupId> |
||||
<artifactId>spring-data-mongodb-cross-store</artifactId> |
||||
</aspectLibrary> |
||||
</aspectLibraries> |
||||
<source>1.6</source> |
||||
<target>1.6</target> |
||||
</configuration> |
||||
</plugin> |
||||
|
||||
... |
||||
|
||||
</plugins> |
||||
</build> |
||||
|
||||
... |
||||
|
||||
</project> |
||||
---- |
||||
==== |
||||
|
||||
Finally, you need to configure your project to use MongoDB and also configure which aspects are used. You should add the following XML snippet to your application context: |
||||
|
||||
.Example application context with MongoDB and cross-store aspect support |
||||
==== |
||||
[source,xml] |
||||
---- |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc" |
||||
xmlns:jpa="http://www.springframework.org/schema/data/jpa" |
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo" |
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo |
||||
https://www.springframework.org/schema/data/mongo/spring-mongo.xsd |
||||
http://www.springframework.org/schema/jdbc |
||||
https://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd |
||||
http://www.springframework.org/schema/beans |
||||
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
||||
http://www.springframework.org/schema/data/jpa |
||||
https://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd"> |
||||
|
||||
... |
||||
|
||||
<!-- Mongo config --> |
||||
<mongo:mongo-client host="localhost" port="27017"/> |
||||
|
||||
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> |
||||
<constructor-arg name="mongoClient" ref="mongoClient"/> |
||||
<constructor-arg name="databaseName" value="test"/> |
||||
<constructor-arg name="defaultCollectionName" value="cross-store"/> |
||||
</bean> |
||||
|
||||
<bean class="org.springframework.data.mongodb.core.MongoExceptionTranslator"/> |
||||
|
||||
<!-- Mongo cross-store aspect config --> |
||||
<bean class="org.springframework.data.persistence.document.mongo.MongoDocumentBacking" |
||||
factory-method="aspectOf"> |
||||
<property name="changeSetPersister" ref="mongoChangeSetPersister"/> |
||||
</bean> |
||||
<bean id="mongoChangeSetPersister" |
||||
class="org.springframework.data.persistence.document.mongo.MongoChangeSetPersister"> |
||||
<property name="mongoTemplate" ref="mongoTemplate"/> |
||||
<property name="entityManagerFactory" ref="entityManagerFactory"/> |
||||
</bean> |
||||
|
||||
... |
||||
|
||||
</beans> |
||||
---- |
||||
==== |
||||
|
||||
[[mongodb_cross-store-application]] |
||||
== Writing the Cross Store Application |
||||
|
||||
We assume that you have a working JPA application, so we cover only the additional steps needed to persist part of your entity in your Mongo database. To do so, you need to identify the field you want to persist. It should be a domain class and follow the general rules for the Mongo mapping support covered in previous chapters. The field you want to persist in MongoDB should be annotated with the `@RelatedDocument` annotation. That is really all you need to do. The cross-store aspects take care of the rest, including: |
||||
|
||||
* Marking the field with `@Transient` so that it will not be persisted by JPA |
||||
* Keeping track of any changes made to the field value and writing them to the database on successful transaction completion |
||||
* Loading the document from MongoDB the first time the value is used in your application. |
||||
|
||||
The following example shows an entity that has a field annotated with `@RelatedDocument`: |
||||
|
||||
.Example of Entity with @RelatedDocument |
||||
==== |
||||
[source,java] |
||||
---- |
||||
@Entity |
||||
public class Customer { |
||||
|
||||
@Id |
||||
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
private Long id; |
||||
|
||||
private String firstName; |
||||
|
||||
private String lastName; |
||||
|
||||
@RelatedDocument |
||||
private SurveyInfo surveyInfo; |
||||
|
||||
// getters and setters omitted |
||||
} |
||||
---- |
||||
==== |
||||
|
||||
The following example shows a domain class that is to be stored as a `Document`: |
||||
|
||||
.Example of a domain class to be stored as a Document |
||||
==== |
||||
[source,java] |
||||
---- |
||||
public class SurveyInfo { |
||||
|
||||
private Map<String, String> questionsAndAnswers; |
||||
|
||||
public SurveyInfo() { |
||||
this.questionsAndAnswers = new HashMap<String, String>(); |
||||
} |
||||
|
||||
public SurveyInfo(Map<String, String> questionsAndAnswers) { |
||||
this.questionsAndAnswers = questionsAndAnswers; |
||||
} |
||||
|
||||
public Map<String, String> getQuestionsAndAnswers() { |
||||
return questionsAndAnswers; |
||||
} |
||||
|
||||
public void setQuestionsAndAnswers(Map<String, String> questionsAndAnswers) { |
||||
this.questionsAndAnswers = questionsAndAnswers; |
||||
} |
||||
|
||||
public SurveyInfo addQuestionAndAnswer(String question, String answer) { |
||||
this.questionsAndAnswers.put(question, answer); |
||||
return this; |
||||
} |
||||
} |
||||
---- |
||||
==== |
||||
|
||||
In the preceding example, once the `SurveyInfo` has been set on the `Customer` object, the `MongoTemplate` that was configured previously is used to save the `SurveyInfo` (along with some metadata about the JPA Entity) in a MongoDB collection named after the fully qualified name of the JPA Entity class. The following code shows how to configure a JPA entity for cross-store persistence with MongoDB: |
||||
|
||||
.Example of code using the JPA Entity configured for cross-store persistence |
||||
==== |
||||
[source,java] |
||||
---- |
||||
Customer customer = new Customer(); |
||||
customer.setFirstName("Sven"); |
||||
customer.setLastName("Olafsen"); |
||||
SurveyInfo surveyInfo = new SurveyInfo() |
||||
.addQuestionAndAnswer("age", "22") |
||||
.addQuestionAndAnswer("married", "Yes") |
||||
.addQuestionAndAnswer("citizenship", "Norwegian"); |
||||
customer.setSurveyInfo(surveyInfo); |
||||
customerRepository.save(customer); |
||||
---- |
||||
==== |
||||
|
||||
Running the preceding above results in the following JSON document being stored in MongoDB: |
||||
|
||||
.Example of JSON document stored in MongoDB |
||||
==== |
||||
[source,javascript] |
||||
---- |
||||
{ "_id" : ObjectId( "4d9e8b6e3c55287f87d4b79e" ), |
||||
"_entity_id" : 1, |
||||
"_entity_class" : "org.springframework.data.mongodb.examples.custsvc.domain.Customer", |
||||
"_entity_field_name" : "surveyInfo", |
||||
"questionsAndAnswers" : { "married" : "Yes", |
||||
"age" : "22", |
||||
"citizenship" : "Norwegian" }, |
||||
"_entity_field_class" : "org.springframework.data.mongodb.examples.custsvc.domain.SurveyInfo" } |
||||
---- |
||||
==== |
||||
Loading…
Reference in new issue