diff --git a/src/docbkx/index.xml b/src/docbkx/index.xml index 2c1f663c2..66b8fcfbc 100644 --- a/src/docbkx/index.xml +++ b/src/docbkx/index.xml @@ -25,7 +25,6 @@ Costin Leau - SpringSource @@ -48,13 +47,15 @@ + - - Reference Documentation + + Mongo DB + \ No newline at end of file diff --git a/src/docbkx/reference/mongo-repositories.xml b/src/docbkx/reference/mongo-repositories.xml new file mode 100644 index 000000000..2f281182f --- /dev/null +++ b/src/docbkx/reference/mongo-repositories.xml @@ -0,0 +1,242 @@ + + + Mongo repositories + + + This chapter will point out the specialties for repository support + for MongoDB. This builds on the core repository support explained in . So make sure you've got a sound understanding + of the basic concepts explained there. + + + To access domain entities stored in a MongoDB you can leverage our + sophisticated repository support that eases implementing those quite + significantly. To do so, simply create an interface for your + repository: + + + Sample Person entity + + public class Person { + + private ObjectId id; + private String firstname; + private String lastname; + + // … getters and setters omitted +} + + + We have a quite simple domain object here. Note that it has a property + named id of type ObjectId. The default + serialization mechanism used in MongoTemplate (which + is backing the repository support) regards properties named id as document + id. Currently we support String, + ObjectId and BigInteger as + id-types. + + + Basic repository interface to persist Person entities + + public interface PersonRepository extends MongoRepository<Person, Long> { + +} + + + The central MongoDB CRUD repository interface is + MongoRepository. Right now this interface + simply serves typing purposes but we will add additional methods to it + later. In your Spring configuration simply add + + + General mongo repository Spring configuration + + <?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"> + + <mongo:repositories base-package="com.acme.*.repositories" + mongo-template-ref="myMongoTemplate" /> + + … + +</beans> + + + This namespace element will cause the base packages to be scanned for + interfaces extending MongoRepository and + create Spring beans for each of them found. By default the repositories will + get a MongoTemplate Spring bean wired that is called + mongoTemplate, so you only need to configure + mongo-template-ref explicitly if you deviate from this + convention. + + MongoRepository extends + PagingAndSortingRepository which you can read + about in . 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 + Persons at a page size of 10 would simply look + something like this: + + + Paging access to Person entities + + @RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +class PersonRepositoryTests { + + @Autowired PersonRepository repository; + + @Test + public void readsFirstPageCorrectly() { + + Page<Person> persons = repository.findAll(new PageRequest(0, 10)); + assertThat(persons.isFirstPage(), is(true)); + } +} + + + The sample creates an application context with Spring's unit test + support which will perform annotation based dependency injection into test + cases. Inside the test method we simply use the repository to query the + datastore. We hand the repository a PageRequest + instance that requests the first page of persons at a page size of + 10. + +
+ Query methods + + Most of the data access operations you usually trigger on a + repository result a query being executed against the Mongo databases. + Defining such a query is just a matter of declaring a method on the + repository interface + + + PersonRepository with query methods + + public interface PersonRepository extends MongoRepository<Person, Long> { + + List<Person> findByLastname(String lastname); + + Page<Person> findByFirstname(String firstname, Pageable pageable); +} + + + The first method shows a query for all people with the given + lastname. The query will be derived parsing the method name for + constraints which can be concatenated with And and + Or. Thus the method name will result in a query + expression of {"lastname" : lastname}. The second example + shows how pagination is applied to a query. Just equip your method + signature with a Pageable parameter and let + the method return a Page instance and we + will automatically page the query accordingly. + + + Supported keywords for query methods + + + + + + + + + + + Keyword + + Sample + + Logical result + + + + + + GreaterThan + + findByAgeGreaterThan(int + age) + + {"age" : {"$gt" : age}} + + + + LessThan + + findByAgeLessThan(int + age) + + {"age" : {"$lt" : age}} + + + + Between + + findByAgeBetween(int from, int + to) + + {"age" : {"$gt" : from, "$lt" : to}} + + + + IsNotNull, + NotNull + + findByFirstnameNotNull() + + {"age" : {"$ne" : null}} + + + + IsNull, + Null + + findByFirstnameNull() + + {"age" : null} + + + + Like + + findByFirstnameLike(String + name) + + {"age" : age} (age as + regex) + + + + (No keyword) + + findByFirstname(String + name) + + {"age" : name} + + + + Not + + findByFirstnameNot(String + name) + + {"age" : {"$ne" : name}} + + + +
+
+
\ No newline at end of file diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index 686248de2..60a1f8f98 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -1,8 +1,8 @@ - - - MongoDB support + + Core support One of the document stores supported by DATADOC is MongoDB. To quote the project home @@ -24,27 +24,18 @@ The MongoDB support provides several components: - - Configuration Factory + Configuration Factory - for configuring + and handling communication with MongoDB via its Java driver - - for configuring and handling communication with MongoDB via its Java driver - + Template implemenattion - providing a + generified, user friendly template classes for interacting with MongoDB. + explains the abstraction builds on + top of the low-level MongoDB Java API to handle the storage and + retrieval of documents plus mapping between documents and domain + classes. - - Template implemenattion - - - providing a generified, user friendly template classes for interacting with MongoDB. - - - - explains the abstraction builds on top of the low-level MongoDB Java API to handle the storage and retrieval of documents plus mapping between documents and domain classes. - - - - Support Classes - - - that offer reusable components such as mapping support and exception translation. - + Support Classes - that offer reusable + components such as mapping support and exception translation. For most tasks, the higher-level abstractions and support services @@ -162,14 +153,17 @@ public class AppConfig { - Collection - Operations + + Collection Operations + MongoDB document operations - Document Operations + + Document Operations + MongoDB collection operations @@ -184,247 +178,6 @@ public class AppConfig { Java-based default converter for most of its operations... -
- Repositories - - To access domain entities stored in a MongoDB you can leverage our - sophisticated repository support that eases implementing those quite - significantly. To do so, simply create an interface for your - repository: - - - Sample Person entity - - public class Person { - - private String firstname; - private String lastname; -} - - - - Basic repository interface to persist Person entities - - public interface PersonRepository extends MongoRepository<Person, Long> { - -} - - - Right now this interface simply serves typing purposes but we will - add additional methods to it later. In your Spring configuration simply - add - - - General mongo repository Spring configuration - - <?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"> - - <mongo:repositories base-package="com.acme.*.repositories" - mongo-template-ref="myMongoTemplate" /> - - ... - -</beans> - - - This namespace element will cause the base packages to be scanned - for interfaces extending MongoRepository - and create Spring beans for each of them found. These Spring beans are - backed by a generic repository implementation that provides you a variety - of useful methods to work with Person - entities. - - - Generic repository interface - - public interface Repository<T, ID extends Serializable> { - - T findById(ID id); - - List<T> findAll(); - - T save(T entity); - - List<T> findAll(Sort sort); - - Page<T> findAll(Pageable pageable); - - // further methods omitted -} - - - We've just listed a brief excerpt of the methods here. As you can - see you get access to basic CRUD operations as well as some more - sophisicated ones that allow programmatic sorting and pagination over the - entities handled by the repository. Working with the repository instance - is just a matter of dependency injecting it into a client. So accessing - the second page of Persons at a page size of 10 - would simply look something like this: - - - Paging access to Person entities - - @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -class PersonRepositoryTests { - - @Autowired PersonRepository repository; - - @Test - public void readsFirstPageCorrectly() { - - Page<Person> persons = repository.findAll(new PageRequest(0, 10)); - assertThat(persons.isFirstPage(), is(true)); - } -} - - - The sample creates an application context with Spring's unit test - support which will perform annotation based dependency injection into test - cases. Inside the test method we simply use the repository to query the - datastore. We hand the repository a PageRequest - instance that requests the first page of persons at a page size of - 10. - -
- Query methods - - Most of the data access operations you usually trigger on a - repository result a query being executed against the Mongo databases. - Defining such a query is just a matter of declaring a method on the - repository interface - - - PersonRepository with query methods - - public interface PersonRepository extends MongoRepository<Person, Long> { - - List<Person> findByLastname(String lastname); - - Page<Person> findByFirstname(String firstname, Pageable pageable); -} - - - The first method shows a query for all people with the given - lastname. The query will be derived parsing the method name for - constraints which can be concatenated with And and - Or. Thus the method name will result in a query - expression of {"lastname" : lastname}. The second example - shows how pagination is applied to a query. Just equip your method - signature with a Pageable parameter and - let the method return a Page instance and - we will automatically page the query accordingly. - - - Supported keywords for query methods - - - - - - - - - - - Keyword - - Sample - - Logical result - - - - - - GreaterThan - - findByAgeGreaterThan(int - age) - - {"age" : {"$gt" : age}} - - - - LessThan - - findByAgeLessThan(int - age) - - {"age" : {"$lt" : age}} - - - - Between - - findByAgeBetween(int from, int - to) - - {"age" : {"$gt" : from, "$lt" : - to}} - - - - IsNotNull, - NotNull - - findByFirstnameNotNull() - - {"age" : {"$ne" : null}} - - - - IsNull, - Null - - findByFirstnameNull() - - {"age" : null} - - - - Like - - findByFirstnameLike(String - name) - - {"age" : age} (age as - regex) - - - - (No keyword) - - findByFirstname(String - name) - - {"age" : name} - - - - Not - - findByFirstnameNot(String - name) - - {"age" : {"$ne" : name}} - - - -
-
-
-
Roadmap ahead @@ -435,4 +188,4 @@ class PersonRepositoryTests { linkend="get-started:help:community">mentioned above, we are interested in hearing from you!
-
+
\ No newline at end of file