8
8
<title >Introduction</title >
9
9
10
10
<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
12
12
linkend =" repositories" />. So make sure you've got a sound understanding
13
13
of the basic concepts explained there.</para >
14
14
</section >
26
26
27
27
<programlisting language =" java" >public class Person {
28
28
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;
33
34
34
- // … getters and setters omitted
35
+ // … getters and setters omitted
35
36
}
36
37
</programlisting >
37
38
</example >
47
48
<example >
48
49
<title >Basic repository interface to persist Person entities</title >
49
50
50
- <programlisting >public interface PersonRepository extends MongoRepository < Person, Long> {
51
+ <programlisting >public interface PersonRepository extends PagingAndSortingRepository < Person, Long> {
51
52
52
- // additional custom finder methods go here
53
+ // additional custom finder methods go here
53
54
}
54
55
</programlisting >
55
56
</example >
56
57
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 >
61
61
62
62
<example >
63
63
<title >General mongo repository Spring configuration</title >
64
64
65
65
<programlisting language =" xml" >< ?xml version="1.0" encoding="UTF-8"?>
66
- < 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">
66
+ < 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">
76
73
77
74
< mongo:mongo id="mongo" />
78
75
79
76
< bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
80
77
< constructor-arg ref="mongo" />
81
- < constructor-arg value="database" />
82
- < constructor-arg value="collection" />
83
- < constructor-arg>
84
- < mongo:mapping-converter />
85
- < /constructor-arg>
78
+ < constructor-arg value="databaseName" />
86
79
< /bean>
87
80
88
- < mongo:repositories base-package="com.acme.*.repositories" mongo-template-ref="myMongoTemplate" />
89
- …
81
+ < mongo:repositories base-package="com.acme.*.repositories" />
90
82
91
83
< /beans> </programlisting >
92
84
</example >
99
91
configure <code >mongo-template-ref</code > explicitly if you deviate from
100
92
this convention.</para >
101
93
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 >
110
101
111
102
<example >
112
103
<title >Paging access to Person entities</title >
@@ -145,7 +136,7 @@ public class PersonRepositoryTests {
145
136
<example >
146
137
<title >PersonRepository with query methods</title >
147
138
148
- <programlisting language =" java" >public interface PersonRepository extends MongoRepository < Person, String> {
139
+ <programlisting language =" java" >public interface PersonRepository extends PagingAndSortingRepository < Person, String> {
149
140
150
141
List< Person> findByLastname(String lastname);
151
142
@@ -192,26 +183,26 @@ public class PersonRepositoryTests {
192
183
<row >
193
184
<entry ><literal >GreaterThan</literal ></entry >
194
185
195
- <entry ><methodname >findByAgeGreaterThan(int age)
196
- </methodname ></entry >
186
+ <entry ><methodname >findByAgeGreaterThan(int
187
+ age) </methodname ></entry >
197
188
198
189
<entry ><code >{"age" : {"$gt" : age}}</code ></entry >
199
190
</row >
200
191
201
192
<row >
202
193
<entry ><literal >LessThan</literal ></entry >
203
194
204
- <entry ><methodname >findByAgeLessThan(int age)
205
- </methodname ></entry >
195
+ <entry ><methodname >findByAgeLessThan(int
196
+ age) </methodname ></entry >
206
197
207
198
<entry ><code >{"age" : {"$lt" : age}}</code ></entry >
208
199
</row >
209
200
210
201
<row >
211
202
<entry ><literal >Between</literal ></entry >
212
203
213
- <entry ><methodname >findByAgeBetween(int from, int to)
214
- </methodname ></entry >
204
+ <entry ><methodname >findByAgeBetween(int from, int
205
+ to) </methodname ></entry >
215
206
216
207
<entry ><code >{"age" : {"$gt" : from, "$lt" : to}}</code ></entry >
217
208
</row >
@@ -237,8 +228,8 @@ public class PersonRepositoryTests {
237
228
<row >
238
229
<entry ><literal >Like</literal ></entry >
239
230
240
- <entry ><methodname >findByFirstnameLike(String name)
241
- </methodname ></entry >
231
+ <entry ><methodname >findByFirstnameLike(String
232
+ name) </methodname ></entry >
242
233
243
234
<entry ><code >{"age" : age}</code > ( <varname >age</varname > as
244
235
regex)</entry >
@@ -247,20 +238,49 @@ public class PersonRepositoryTests {
247
238
<row >
248
239
<entry >(No keyword)</entry >
249
240
250
- <entry ><methodname >findByFirstname(String name)
251
- </methodname ></entry >
241
+ <entry ><methodname >findByFirstname(String
242
+ name) </methodname ></entry >
252
243
253
244
<entry ><code >{"age" : name}</code ></entry >
254
245
</row >
255
246
256
247
<row >
257
248
<entry ><literal >Not</literal ></entry >
258
249
259
- <entry ><methodname >findByFirstnameNot(String name)
260
- </methodname ></entry >
250
+ <entry ><methodname >findByFirstnameNot(String
251
+ name) </methodname ></entry >
261
252
262
253
<entry ><code >{"age" : {"$ne" : name}}</code ></entry >
263
254
</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 >
264
284
</tbody >
265
285
</tgroup >
266
286
</table ></para >
@@ -289,7 +309,7 @@ public class PersonRepositoryTests {
289
309
290
310
<programlisting language =" java" >public interface PersonRepository extends MongoRepository< Person, String>
291
311
292
- @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname': 1, 'lastname': 1}")
312
+ @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
293
313
List< Person> findByThePersonsFirstname(String firstname);
294
314
295
315
}</programlisting >
@@ -389,4 +409,4 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"),
389
409
MongoDB queries.</para >
390
410
</section >
391
411
</section >
392
- </chapter >
412
+ </chapter >
0 commit comments