|
1 | 1 | <?xml version="1.0" encoding="UTF-8"?>
|
2 | 2 | <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
|
3 |
| - "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> |
| 3 | +"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> |
4 | 4 | <chapter id="mongo.cross.store">
|
5 | 5 | <title>Cross Store support</title>
|
6 | 6 |
|
7 |
| - <para>Cross Store support introduction. |
8 |
| - </para> |
| 7 | + <para>Sometimes you need to store data in multiple data stores and these |
| 8 | + data stores can be of different types. One might be relational while the |
| 9 | + other a document store. For this use case we have created a separate module |
| 10 | + in the MongoDB support that handles what we call cross-store support. The |
| 11 | + current implemenatation is based on JPA as the driver for the relational |
| 12 | + database and we allow select fields in the Entities to be stored in a Mongo |
| 13 | + database. In addition to allowing you to store your data in two stores we |
| 14 | + also coordinate persistence operations for the non-transactional MongoDB |
| 15 | + store with the transaction life-cycle for the relational database.</para> |
9 | 16 |
|
10 |
| - <section id="mongodb:croos-store-configuration"> |
| 17 | + <section id="mongodb_cross-store-configuration"> |
11 | 18 | <title>Cross Store Configuration</title>
|
12 | 19 |
|
13 |
| - <para>Cross Store... |
14 |
| - </para> |
| 20 | + <para>Assuming that you have a working JPA application and would like to |
| 21 | + add some cross-store persistence for MongoDB. What do you have to add to |
| 22 | + your configuration?</para> |
15 | 23 |
|
| 24 | + <para>First of all you need to add a dependency on the |
| 25 | + <filename>spring-data-mongodb-cross-store</filename> module. Using Maven |
| 26 | + this is done by adding a dependency to your pom:</para> |
| 27 | + |
| 28 | + <example> |
| 29 | + <title>Example Maven pom.xml with spring-data-mongodb-cross-store |
| 30 | + dependency</title> |
| 31 | + |
| 32 | + <programlisting language="xml"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 33 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 34 | + <modelVersion>4.0.0</modelVersion> |
| 35 | + |
| 36 | +... |
| 37 | + |
| 38 | + <!-- Spring Data --> |
| 39 | + <dependency> |
| 40 | + <groupId>org.springframework.data</groupId> |
| 41 | + <artifactId>spring-data-mongodb-cross-store</artifactId> |
| 42 | + <version>${spring.data.mongo.version}</version> |
| 43 | + </dependency> |
| 44 | + |
| 45 | + |
| 46 | +... |
| 47 | + |
| 48 | +</project> |
| 49 | +</programlisting> |
| 50 | + </example> |
| 51 | + |
| 52 | + <para>Once this is done we need to enable AspectJ for the project. The |
| 53 | + cross-store support is implemented using AspectJ aspects so by enabling |
| 54 | + compile time AspectJ support the cross-store features will become |
| 55 | + available to your project. In Maven you would add an additional plugin to |
| 56 | + the <build> section of the pom:</para> |
| 57 | + |
| 58 | + <example> |
| 59 | + <title>Example Maven pom.xml with AspectJ plugin enabled</title> |
| 60 | + |
| 61 | + <programlisting language="xml"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 62 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 63 | + <modelVersion>4.0.0</modelVersion> |
| 64 | + |
| 65 | +... |
| 66 | + |
| 67 | + <build> |
| 68 | + <plugins> |
| 69 | + |
| 70 | + ... |
| 71 | + |
| 72 | + <plugin> |
| 73 | + <groupId>org.codehaus.mojo</groupId> |
| 74 | + <artifactId>aspectj-maven-plugin</artifactId> |
| 75 | + <version>1.0</version> |
| 76 | + <dependencies> |
| 77 | + <!-- NB: You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) --> |
| 78 | + <dependency> |
| 79 | + <groupId>org.aspectj</groupId> |
| 80 | + <artifactId>aspectjrt</artifactId> |
| 81 | + <version>${aspectj.version}</version> |
| 82 | + </dependency> |
| 83 | + <dependency> |
| 84 | + <groupId>org.aspectj</groupId> |
| 85 | + <artifactId>aspectjtools</artifactId> |
| 86 | + <version>${aspectj.version}</version> |
| 87 | + </dependency> |
| 88 | + </dependencies> |
| 89 | + <executions> |
| 90 | + <execution> |
| 91 | + <goals> |
| 92 | + <goal>compile</goal> |
| 93 | + <goal>test-compile</goal> |
| 94 | + </goals> |
| 95 | + </execution> |
| 96 | + </executions> |
| 97 | + <configuration> |
| 98 | + <outxml>true</outxml> |
| 99 | + <aspectLibraries> |
| 100 | + <aspectLibrary> |
| 101 | + <groupId>org.springframework</groupId> |
| 102 | + <artifactId>spring-aspects</artifactId> |
| 103 | + </aspectLibrary> |
| 104 | + <aspectLibrary> |
| 105 | + <groupId>org.springframework.data</groupId> |
| 106 | + <artifactId>spring-data-mongodb-cross-store</artifactId> |
| 107 | + </aspectLibrary> |
| 108 | + </aspectLibraries> |
| 109 | + <source>1.6</source> |
| 110 | + <target>1.6</target> |
| 111 | + </configuration> |
| 112 | + </plugin> |
| 113 | + |
| 114 | + ... |
| 115 | + |
| 116 | + </plugins> |
| 117 | + </build> |
| 118 | + |
| 119 | +... |
| 120 | + |
| 121 | +</project> |
| 122 | +</programlisting> |
| 123 | + </example> |
| 124 | + |
| 125 | + <para>Finally, you need to configure your project to use MomgoDB and also |
| 126 | + configure the aspects that are used. The following XML snippet should be |
| 127 | + added to your application context:</para> |
| 128 | + |
| 129 | + <example> |
| 130 | + <title>Example application context with MongoDB and cross-store aspect |
| 131 | + support</title> |
| 132 | + |
| 133 | + <programlisting language="xml"><?xml version="1.0" encoding="UTF-8"?> |
| 134 | +<beans xmlns="http://www.springframework.org/schema/beans" |
| 135 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 136 | + xmlns:jdbc="http://www.springframework.org/schema/jdbc" |
| 137 | + xmlns:jpa="http://www.springframework.org/schema/data/jpa" |
| 138 | + xmlns:mongo="http://www.springframework.org/schema/data/mongo" |
| 139 | + xsi:schemaLocation="http://www.springframework.org/schema/data/mongo |
| 140 | + http://www.springframework.org/schema/data/mongo/spring-mongo.xsd |
| 141 | + http://www.springframework.org/schema/jdbc |
| 142 | + http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd |
| 143 | + http://www.springframework.org/schema/beans |
| 144 | + http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
| 145 | + http://www.springframework.org/schema/data/jpa |
| 146 | + http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd"> |
| 147 | + |
| 148 | + ... |
| 149 | + |
| 150 | + <!-- Mongo config --> |
| 151 | + <mongo:mongo host="localhost" port="27017"/> |
| 152 | + |
| 153 | + <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> |
| 154 | + <constructor-arg name="mongo" ref="mongo"/> |
| 155 | + <constructor-arg name="databaseName" value="test"/> |
| 156 | + <constructor-arg name="defaultCollectionName" value="cross-store"/> |
| 157 | + </bean> |
| 158 | + |
| 159 | + <bean class="org.springframework.data.document.mongodb.MongoExceptionTranslator"/> |
| 160 | + |
| 161 | + <!-- Mongo cross-store aspect config --> |
| 162 | + <bean class="org.springframework.data.persistence.document.mongo.MongoDocumentBacking" |
| 163 | + factory-method="aspectOf"> |
| 164 | + <property name="changeSetPersister" ref="mongoChangeSetPersister"/> |
| 165 | + </bean> |
| 166 | + <bean id="mongoChangeSetPersister" |
| 167 | + class="org.springframework.data.persistence.document.mongo.MongoChangeSetPersister"> |
| 168 | + <property name="mongoTemplate" ref="mongoTemplate"/> |
| 169 | + <property name="entityManagerFactory" ref="entityManagerFactory"/> |
| 170 | + </bean> |
| 171 | + |
| 172 | + ... |
| 173 | + |
| 174 | +</beans> |
| 175 | +</programlisting> |
| 176 | + </example> |
| 177 | + |
| 178 | + <para></para> |
| 179 | + |
| 180 | + <para> </para> |
| 181 | + </section> |
| 182 | + |
| 183 | + <section id="mongodb_cross-store-application"> |
| 184 | + <title>Writing the Cross Store Application</title> |
| 185 | + |
| 186 | + <para>We are assuming that you have a working JPA application so we will |
| 187 | + only cover the additional steps needed to persist part of your Entity in |
| 188 | + your Mongo database. First you need to identify the field you want |
| 189 | + persited. It should be a domain class and follow the general rules for the |
| 190 | + Mongo mapping support covered in previous chapters. The filed you want |
| 191 | + persisted in MongoDB should be annotated using the |
| 192 | + <classname>@RelatedDocument</classname> annotation. Well, that is really |
| 193 | + all you need to do. The cross-store aspects take care of the rest. This |
| 194 | + include marking the field with @Transient so it won't be persisted using |
| 195 | + JPA, keeping track of any changes and flushing them on succesfull |
| 196 | + transaction completion, loading teh document for MongoDB when the values |
| 197 | + is used in your application. Here is an example of a simple Entity that |
| 198 | + has a field annotated with @RelatedEntity.</para> |
| 199 | + |
| 200 | + <example> |
| 201 | + <title>Example of Entity with @RelatedDocument</title> |
| 202 | + |
| 203 | + <programlisting language="java">@Entity |
| 204 | +public class Customer { |
| 205 | + |
| 206 | + @Id |
| 207 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 208 | + private Long id; |
| 209 | + |
| 210 | + private String firstName; |
| 211 | + |
| 212 | + private String lastName; |
| 213 | + |
| 214 | + @RelatedDocument |
| 215 | + private SurveyInfo surveyInfo; |
| 216 | + |
| 217 | + // getters and setters omitted |
| 218 | + |
| 219 | +} </programlisting> |
| 220 | + </example> |
| 221 | + |
| 222 | + <example> |
| 223 | + <title>Example of domain class to be stored as document</title> |
| 224 | + |
| 225 | + <programlisting language="java">public class SurveyInfo { |
| 226 | + |
| 227 | + private Map<String, String> questionsAndAnswers; |
| 228 | + |
| 229 | + public Map<String, String> getQuestionsAndAnswers() { |
| 230 | + return questionsAndAnswers; |
| 231 | + } |
| 232 | + |
| 233 | + public void setQuestionsAndAnswers(Map<String, String> questionsAndAnswers) { |
| 234 | + this.questionsAndAnswers = questionsAndAnswers; |
| 235 | + } |
| 236 | +} </programlisting> |
| 237 | + </example> |
| 238 | + |
| 239 | + <para>Once t...</para> |
| 240 | + |
| 241 | + <para></para> |
| 242 | + |
| 243 | + <para></para> |
| 244 | + |
| 245 | + <para></para> |
16 | 246 | </section>
|
17 | 247 | </chapter>
|
0 commit comments