Updated reference documentation for Mongo repositories.

This commit is contained in:
Oliver Gierke
2011-05-24 17:36:00 +02:00
parent 4d33c9c360
commit de06029ea2

View File

@@ -8,7 +8,7 @@
<title>Introduction</title>
<para>This chapter will point out the specialties for repository support
for MongoDB. This builds on the core repository support explained in<xref
for MongoDB. This builds on the core repository support explained in <xref
linkend="repositories" />. So make sure you've got a sound understanding
of the basic concepts explained there.</para>
</section>
@@ -26,12 +26,13 @@
<programlisting language="java">public class Person {
private String id;
private String firstname;
private String lastname;
private Address address;
@Id
private String id;
private String firstname;
private String lastname;
private Address address;
// … getters and setters omitted
// … getters and setters omitted
}
</programlisting>
</example>
@@ -47,46 +48,37 @@
<example>
<title>Basic repository interface to persist Person entities</title>
<programlisting>public interface PersonRepository extends MongoRepository&lt;Person, Long&gt; {
<programlisting>public interface PersonRepository extends PagingAndSortingRepository&lt;Person, Long&gt; {
// additional custom finder methods go here
// additional custom finder methods go here
}
</programlisting>
</example>
<para>The central MongoDB CRUD repository interface is
<interfacename>MongoRepository</interfacename>. Right now this interface
simply serves typing purposes but we will add additional methods to it
later. In your Spring configuration simply add</para>
<para>Right now this interface simply serves typing purposes but we will
add additional methods to it later. In your Spring configuration simply
add</para>
<example>
<title>General mongo repository Spring configuration</title>
<programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"&gt;
&lt;mongo:mongo id="mongo" /&gt;
&lt;bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"&gt;
&lt;constructor-arg ref="mongo" /&gt;
&lt;constructor-arg value="database" /&gt;
&lt;constructor-arg value="collection" /&gt;
&lt;constructor-arg&gt;
&lt;mongo:mapping-converter /&gt;
&lt;/constructor-arg&gt;
&lt;constructor-arg value="databaseName" /&gt;
&lt;/bean&gt;
&lt;mongo:repositories base-package="com.acme.*.repositories" mongo-template-ref="myMongoTemplate" /&gt;
&lt;mongo:repositories base-package="com.acme.*.repositories" /&gt;
&lt;/beans&gt;</programlisting>
</example>
@@ -99,14 +91,13 @@
configure <code>mongo-template-ref</code> explicitly if you deviate from
this convention.</para>
<para><interfacename>MongoRepository</interfacename> extends
<interfacename>PagingAndSortingRepository</interfacename> which you can
read about in<xref linkend="repositories.repository" />. In general it
provides you with CRUD operations as well as methods for paginated and
sorted access to the entities. Working with the repository instance is
just a matter of dependency injecting it into a client. So accessing the
second page of <classname>Person</classname>s at a page size of 10 would
simply look something like this:</para>
<para>As our domain repository extends
<interfacename>PagingAndSortingRepository</interfacename> it provides you
with CRUD operations as well as methods for paginated and sorted access to
the entities. Working with the repository instance is just a matter of
dependency injecting it into a client. So accessing the second page of
<classname>Person</classname>s at a page size of 10 would simply look
something like this:</para>
<example>
<title>Paging access to Person entities</title>
@@ -145,7 +136,7 @@ public class PersonRepositoryTests {
<example>
<title>PersonRepository with query methods</title>
<programlisting language="java">public interface PersonRepository extends MongoRepository&lt;Person, String&gt; {
<programlisting language="java">public interface PersonRepository extends PagingAndSortingRepository&lt;Person, String&gt; {
List&lt;Person&gt; findByLastname(String lastname);
@@ -192,8 +183,8 @@ public class PersonRepositoryTests {
<row>
<entry><literal>GreaterThan</literal></entry>
<entry><methodname>findByAgeGreaterThan(int age)
</methodname></entry>
<entry><methodname>findByAgeGreaterThan(int
age)</methodname></entry>
<entry><code>{"age" : {"$gt" : age}}</code></entry>
</row>
@@ -201,8 +192,8 @@ public class PersonRepositoryTests {
<row>
<entry><literal>LessThan</literal></entry>
<entry><methodname>findByAgeLessThan(int age)
</methodname></entry>
<entry><methodname>findByAgeLessThan(int
age)</methodname></entry>
<entry><code>{"age" : {"$lt" : age}}</code></entry>
</row>
@@ -210,8 +201,8 @@ public class PersonRepositoryTests {
<row>
<entry><literal>Between</literal></entry>
<entry><methodname>findByAgeBetween(int from, int to)
</methodname></entry>
<entry><methodname>findByAgeBetween(int from, int
to)</methodname></entry>
<entry><code>{"age" : {"$gt" : from, "$lt" : to}}</code></entry>
</row>
@@ -237,8 +228,8 @@ public class PersonRepositoryTests {
<row>
<entry><literal>Like</literal></entry>
<entry><methodname>findByFirstnameLike(String name)
</methodname></entry>
<entry><methodname>findByFirstnameLike(String
name)</methodname></entry>
<entry><code>{"age" : age}</code> ( <varname>age</varname> as
regex)</entry>
@@ -247,8 +238,8 @@ public class PersonRepositoryTests {
<row>
<entry>(No keyword)</entry>
<entry><methodname>findByFirstname(String name)
</methodname></entry>
<entry><methodname>findByFirstname(String
name)</methodname></entry>
<entry><code>{"age" : name}</code></entry>
</row>
@@ -256,11 +247,40 @@ public class PersonRepositoryTests {
<row>
<entry><literal>Not</literal></entry>
<entry><methodname>findByFirstnameNot(String name)
</methodname></entry>
<entry><methodname>findByFirstnameNot(String
name)</methodname></entry>
<entry><code>{"age" : {"$ne" : name}}</code></entry>
</row>
<row>
<entry><literal>Near</literal></entry>
<entry><methodname>findByLocationNear(Point
point)</methodname></entry>
<entry><code>{"location" : {"$near" : [x,y]}}</code></entry>
</row>
<row>
<entry><literal>Within</literal></entry>
<entry><methodname>findByLocationWithin(Circle
circle)</methodname></entry>
<entry><code>{"location" : {"$within" : {"$center" : [ [x, y],
distance]}}}</code></entry>
</row>
<row>
<entry><literal>Within</literal></entry>
<entry><methodname>findByLocationWithin(Box
box)</methodname></entry>
<entry><code>{"location" : {"$within" : {"$box" : [ [x1, y1],
x2, y2]}}}</code></entry>
</row>
</tbody>
</tgroup>
</table></para>
@@ -289,7 +309,7 @@ public class PersonRepositoryTests {
<programlisting language="java">public interface PersonRepository extends MongoRepository&lt;Person, String&gt;
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname': 1, 'lastname': 1}")
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
List&lt;Person&gt; findByThePersonsFirstname(String firstname);
}</programlisting>
@@ -389,4 +409,4 @@ Page&lt;Person&gt; page = repository.findAll(person.lastname.contains("a"),
MongoDB queries.</para>
</section>
</section>
</chapter>
</chapter>