Skip to content

Commit de06029

Browse files
committed
Updated reference documentation for Mongo repositories.
1 parent 4d33c9c commit de06029

File tree

1 file changed

+72
-52
lines changed

1 file changed

+72
-52
lines changed

src/docbkx/reference/mongo-repositories.xml

Lines changed: 72 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<title>Introduction</title>
99

1010
<para>This chapter will point out the specialties for repository support
11-
for MongoDB. This builds on the core repository support explained in<xref
11+
for MongoDB. This builds on the core repository support explained in <xref
1212
linkend="repositories" />. So make sure you've got a sound understanding
1313
of the basic concepts explained there.</para>
1414
</section>
@@ -26,12 +26,13 @@
2626

2727
<programlisting language="java">public class Person {
2828

29-
private String id;
30-
private String firstname;
31-
private String lastname;
32-
private Address address;
29+
@Id
30+
private String id;
31+
private String firstname;
32+
private String lastname;
33+
private Address address;
3334

34-
// … getters and setters omitted
35+
// … getters and setters omitted
3536
}
3637
</programlisting>
3738
</example>
@@ -47,46 +48,37 @@
4748
<example>
4849
<title>Basic repository interface to persist Person entities</title>
4950

50-
<programlisting>public interface PersonRepository extends MongoRepository&lt;Person, Long&gt; {
51+
<programlisting>public interface PersonRepository extends PagingAndSortingRepository&lt;Person, Long&gt; {
5152

52-
// additional custom finder methods go here
53+
// additional custom finder methods go here
5354
}
5455
</programlisting>
5556
</example>
5657

57-
<para>The central MongoDB CRUD repository interface is
58-
<interfacename>MongoRepository</interfacename>. Right now this interface
59-
simply serves typing purposes but we will add additional methods to it
60-
later. In your Spring configuration simply add</para>
58+
<para>Right now this interface simply serves typing purposes but we will
59+
add additional methods to it later. In your Spring configuration simply
60+
add</para>
6161

6262
<example>
6363
<title>General mongo repository Spring configuration</title>
6464

6565
<programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
66-
&lt;beans xmlns="http://www.springframework.org/schema/beans"
67-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
68-
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
69-
xmlns:context="http://www.springframework.org/schema/context"
70-
xsi:schemaLocation="http://www.springframework.org/schema/beans
71-
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
72-
http://www.springframework.org/schema/data/mongo
73-
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
74-
http://www.springframework.org/schema/context
75-
http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
66+
&lt;beans xmlns="http://www.springframework.org/schema/beans"
67+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
68+
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
69+
xsi:schemaLocation="http://www.springframework.org/schema/beans
70+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
71+
http://www.springframework.org/schema/data/mongo
72+
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"&gt;
7673

7774
&lt;mongo:mongo id="mongo" /&gt;
7875

7976
&lt;bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"&gt;
8077
&lt;constructor-arg ref="mongo" /&gt;
81-
&lt;constructor-arg value="database" /&gt;
82-
&lt;constructor-arg value="collection" /&gt;
83-
&lt;constructor-arg&gt;
84-
&lt;mongo:mapping-converter /&gt;
85-
&lt;/constructor-arg&gt;
78+
&lt;constructor-arg value="databaseName" /&gt;
8679
&lt;/bean&gt;
8780

88-
&lt;mongo:repositories base-package="com.acme.*.repositories" mongo-template-ref="myMongoTemplate" /&gt;
89-
81+
&lt;mongo:repositories base-package="com.acme.*.repositories" /&gt;
9082

9183
&lt;/beans&gt;</programlisting>
9284
</example>
@@ -99,14 +91,13 @@
9991
configure <code>mongo-template-ref</code> explicitly if you deviate from
10092
this convention.</para>
10193

102-
<para><interfacename>MongoRepository</interfacename> extends
103-
<interfacename>PagingAndSortingRepository</interfacename> which you can
104-
read about in<xref linkend="repositories.repository" />. In general it
105-
provides you with CRUD operations as well as methods for paginated and
106-
sorted access to the entities. Working with the repository instance is
107-
just a matter of dependency injecting it into a client. So accessing the
108-
second page of <classname>Person</classname>s at a page size of 10 would
109-
simply look something like this:</para>
94+
<para>As our domain repository extends
95+
<interfacename>PagingAndSortingRepository</interfacename> it provides you
96+
with CRUD operations as well as methods for paginated and sorted access to
97+
the entities. Working with the repository instance is just a matter of
98+
dependency injecting it into a client. So accessing the second page of
99+
<classname>Person</classname>s at a page size of 10 would simply look
100+
something like this:</para>
110101

111102
<example>
112103
<title>Paging access to Person entities</title>
@@ -145,7 +136,7 @@ public class PersonRepositoryTests {
145136
<example>
146137
<title>PersonRepository with query methods</title>
147138

148-
<programlisting language="java">public interface PersonRepository extends MongoRepository&lt;Person, String&gt; {
139+
<programlisting language="java">public interface PersonRepository extends PagingAndSortingRepository&lt;Person, String&gt; {
149140

150141
List&lt;Person&gt; findByLastname(String lastname);
151142

@@ -192,26 +183,26 @@ public class PersonRepositoryTests {
192183
<row>
193184
<entry><literal>GreaterThan</literal></entry>
194185

195-
<entry><methodname>findByAgeGreaterThan(int age)
196-
</methodname></entry>
186+
<entry><methodname>findByAgeGreaterThan(int
187+
age)</methodname></entry>
197188

198189
<entry><code>{"age" : {"$gt" : age}}</code></entry>
199190
</row>
200191

201192
<row>
202193
<entry><literal>LessThan</literal></entry>
203194

204-
<entry><methodname>findByAgeLessThan(int age)
205-
</methodname></entry>
195+
<entry><methodname>findByAgeLessThan(int
196+
age)</methodname></entry>
206197

207198
<entry><code>{"age" : {"$lt" : age}}</code></entry>
208199
</row>
209200

210201
<row>
211202
<entry><literal>Between</literal></entry>
212203

213-
<entry><methodname>findByAgeBetween(int from, int to)
214-
</methodname></entry>
204+
<entry><methodname>findByAgeBetween(int from, int
205+
to)</methodname></entry>
215206

216207
<entry><code>{"age" : {"$gt" : from, "$lt" : to}}</code></entry>
217208
</row>
@@ -237,8 +228,8 @@ public class PersonRepositoryTests {
237228
<row>
238229
<entry><literal>Like</literal></entry>
239230

240-
<entry><methodname>findByFirstnameLike(String name)
241-
</methodname></entry>
231+
<entry><methodname>findByFirstnameLike(String
232+
name)</methodname></entry>
242233

243234
<entry><code>{"age" : age}</code> ( <varname>age</varname> as
244235
regex)</entry>
@@ -247,20 +238,49 @@ public class PersonRepositoryTests {
247238
<row>
248239
<entry>(No keyword)</entry>
249240

250-
<entry><methodname>findByFirstname(String name)
251-
</methodname></entry>
241+
<entry><methodname>findByFirstname(String
242+
name)</methodname></entry>
252243

253244
<entry><code>{"age" : name}</code></entry>
254245
</row>
255246

256247
<row>
257248
<entry><literal>Not</literal></entry>
258249

259-
<entry><methodname>findByFirstnameNot(String name)
260-
</methodname></entry>
250+
<entry><methodname>findByFirstnameNot(String
251+
name)</methodname></entry>
261252

262253
<entry><code>{"age" : {"$ne" : name}}</code></entry>
263254
</row>
255+
256+
<row>
257+
<entry><literal>Near</literal></entry>
258+
259+
<entry><methodname>findByLocationNear(Point
260+
point)</methodname></entry>
261+
262+
<entry><code>{"location" : {"$near" : [x,y]}}</code></entry>
263+
</row>
264+
265+
<row>
266+
<entry><literal>Within</literal></entry>
267+
268+
<entry><methodname>findByLocationWithin(Circle
269+
circle)</methodname></entry>
270+
271+
<entry><code>{"location" : {"$within" : {"$center" : [ [x, y],
272+
distance]}}}</code></entry>
273+
</row>
274+
275+
<row>
276+
<entry><literal>Within</literal></entry>
277+
278+
<entry><methodname>findByLocationWithin(Box
279+
box)</methodname></entry>
280+
281+
<entry><code>{"location" : {"$within" : {"$box" : [ [x1, y1],
282+
x2, y2]}}}</code></entry>
283+
</row>
264284
</tbody>
265285
</tgroup>
266286
</table></para>
@@ -289,7 +309,7 @@ public class PersonRepositoryTests {
289309

290310
<programlisting language="java">public interface PersonRepository extends MongoRepository&lt;Person, String&gt;
291311

292-
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname': 1, 'lastname': 1}")
312+
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
293313
List&lt;Person&gt; findByThePersonsFirstname(String firstname);
294314

295315
}</programlisting>
@@ -389,4 +409,4 @@ Page&lt;Person&gt; page = repository.findAll(person.lastname.contains("a"),
389409
MongoDB queries.</para>
390410
</section>
391411
</section>
392-
</chapter>
412+
</chapter>

0 commit comments

Comments
 (0)