Updated reference documentation for Mongo repositories.
This commit is contained in:
@@ -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<Person, Long> {
|
||||
<programlisting>public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
|
||||
|
||||
// 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"><?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: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">
|
||||
<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">
|
||||
|
||||
<mongo:mongo id="mongo" />
|
||||
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||
<constructor-arg ref="mongo" />
|
||||
<constructor-arg value="database" />
|
||||
<constructor-arg value="collection" />
|
||||
<constructor-arg>
|
||||
<mongo:mapping-converter />
|
||||
</constructor-arg>
|
||||
<constructor-arg value="databaseName" />
|
||||
</bean>
|
||||
|
||||
<mongo:repositories base-package="com.acme.*.repositories" mongo-template-ref="myMongoTemplate" />
|
||||
…
|
||||
<mongo:repositories base-package="com.acme.*.repositories" />
|
||||
|
||||
</beans></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<Person, String> {
|
||||
<programlisting language="java">public interface PersonRepository extends PagingAndSortingRepository<Person, String> {
|
||||
|
||||
List<Person> 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<Person, String>
|
||||
|
||||
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname': 1, 'lastname': 1}")
|
||||
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
|
||||
List<Person> findByThePersonsFirstname(String firstname);
|
||||
|
||||
}</programlisting>
|
||||
@@ -389,4 +409,4 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"),
|
||||
MongoDB queries.</para>
|
||||
</section>
|
||||
</section>
|
||||
</chapter>
|
||||
</chapter>
|
||||
Reference in New Issue
Block a user