|
|
|
|
@ -14,7 +14,7 @@
@@ -14,7 +14,7 @@
|
|
|
|
|
also coordinate persistence operations for the non-transactional MongoDB |
|
|
|
|
store with the transaction life-cycle for the relational database.</para> |
|
|
|
|
|
|
|
|
|
<section id="mongodb:croos-store-configuration"> |
|
|
|
|
<section id="mongodb_cross-store-configuration"> |
|
|
|
|
<title>Cross Store Configuration</title> |
|
|
|
|
|
|
|
|
|
<para>Assuming that you have a working JPA application and would like to |
|
|
|
|
@ -26,7 +26,8 @@
@@ -26,7 +26,8 @@
|
|
|
|
|
this is done by adding a dependency to your pom:</para> |
|
|
|
|
|
|
|
|
|
<example> |
|
|
|
|
<title>Example Maven pom.xml</title> |
|
|
|
|
<title>Example Maven pom.xml with spring-data-mongodb-cross-store |
|
|
|
|
dependency</title> |
|
|
|
|
|
|
|
|
|
<programlisting language="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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
|
|
|
@ -48,6 +49,199 @@
@@ -48,6 +49,199 @@
|
|
|
|
|
</programlisting> |
|
|
|
|
</example> |
|
|
|
|
|
|
|
|
|
<para>Once this is done we need to enable AspectJ for the project. The |
|
|
|
|
cross-store support is implemented using AspectJ aspects so by enabling |
|
|
|
|
compile time AspectJ support the cross-store features will become |
|
|
|
|
available to your project. In Maven you would add an additional plugin to |
|
|
|
|
the <build> section of the pom:</para> |
|
|
|
|
|
|
|
|
|
<example> |
|
|
|
|
<title>Example Maven pom.xml with AspectJ plugin enabled</title> |
|
|
|
|
|
|
|
|
|
<programlisting language="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 http://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> |
|
|
|
|
</programlisting> |
|
|
|
|
</example> |
|
|
|
|
|
|
|
|
|
<para>Finally, you need to configure your project to use MomgoDB and also |
|
|
|
|
configure the aspects that are used. The following XML snippet should be |
|
|
|
|
added to your application context:</para> |
|
|
|
|
|
|
|
|
|
<example> |
|
|
|
|
<title>Example application context with MongoDB and cross-store aspect |
|
|
|
|
support</title> |
|
|
|
|
|
|
|
|
|
<programlisting language="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 |
|
|
|
|
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd |
|
|
|
|
http://www.springframework.org/schema/jdbc |
|
|
|
|
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd |
|
|
|
|
http://www.springframework.org/schema/beans |
|
|
|
|
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
|
|
|
|
http://www.springframework.org/schema/data/jpa |
|
|
|
|
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd"> |
|
|
|
|
|
|
|
|
|
... |
|
|
|
|
|
|
|
|
|
<!-- Mongo config --> |
|
|
|
|
<mongo:mongo host="localhost" port="27017"/> |
|
|
|
|
|
|
|
|
|
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> |
|
|
|
|
<constructor-arg name="mongo" ref="mongo"/> |
|
|
|
|
<constructor-arg name="databaseName" value="test"/> |
|
|
|
|
<constructor-arg name="defaultCollectionName" value="cross-store"/> |
|
|
|
|
</bean> |
|
|
|
|
|
|
|
|
|
<bean class="org.springframework.data.document.mongodb.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> |
|
|
|
|
</programlisting> |
|
|
|
|
</example> |
|
|
|
|
|
|
|
|
|
<para></para> |
|
|
|
|
|
|
|
|
|
<para> </para> |
|
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
<section id="mongodb_cross-store-application"> |
|
|
|
|
<title>Writing the Cross Store Application</title> |
|
|
|
|
|
|
|
|
|
<para>We are assuming that you have a working JPA application so we will |
|
|
|
|
only cover the additional steps needed to persist part of your Entity in |
|
|
|
|
your Mongo database. First you need to identify the field you want |
|
|
|
|
persited. It should be a domain class and follow the general rules for the |
|
|
|
|
Mongo mapping support covered in previous chapters. The filed you want |
|
|
|
|
persisted in MongoDB should be annotated using the |
|
|
|
|
<classname>@RelatedDocument</classname> annotation. Well, that is really |
|
|
|
|
all you need to do. The cross-store aspects take care of the rest. This |
|
|
|
|
include marking the field with @Transient so it won't be persisted using |
|
|
|
|
JPA, keeping track of any changes and flushing them on succesfull |
|
|
|
|
transaction completion, loading teh document for MongoDB when the values |
|
|
|
|
is used in your application. Here is an example of a simple Entity that |
|
|
|
|
has a field annotated with @RelatedEntity.</para> |
|
|
|
|
|
|
|
|
|
<example> |
|
|
|
|
<title>Example of Entity with @RelatedDocument</title> |
|
|
|
|
|
|
|
|
|
<programlisting language="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 |
|
|
|
|
|
|
|
|
|
} </programlisting> |
|
|
|
|
</example> |
|
|
|
|
|
|
|
|
|
<example> |
|
|
|
|
<title>Example of domain class to be stored as document</title> |
|
|
|
|
|
|
|
|
|
<programlisting language="java">public class SurveyInfo { |
|
|
|
|
|
|
|
|
|
private Map<String, String> questionsAndAnswers; |
|
|
|
|
|
|
|
|
|
public Map<String, String> getQuestionsAndAnswers() { |
|
|
|
|
return questionsAndAnswers; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setQuestionsAndAnswers(Map<String, String> questionsAndAnswers) { |
|
|
|
|
this.questionsAndAnswers = questionsAndAnswers; |
|
|
|
|
} |
|
|
|
|
} </programlisting> |
|
|
|
|
</example> |
|
|
|
|
|
|
|
|
|
<para>Once t...</para> |
|
|
|
|
|
|
|
|
|
<para></para> |
|
|
|
|
|
|
|
|
|
<para></para> |
|
|
|
|
|
|
|
|
|
<para></para> |
|
|
|
|
</section> |
|
|
|
|
</chapter> |
|
|
|
|
|