13.07.2015 Views

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

please define productname in your docbook file!Type-safe Query methods<strong>MongoDB</strong> repository support integrates with the QueryDSL project which provides a means to performtype-safe queries in Java. To quote from the project description, "Instead of writing queries as inlinestrings or externalizing them into XML files they are constructed via a fluent API." It provides the followingfeatures• Code completion in IDE (all properties, methods and operations can be expanded in your favoriteJava IDE)• Almost no syntactically invalid queries allowed (type-safe on all levels)• Domain types and properties can be referenced safely (no Strings involved!)• Adopts better to refactoring changes in domain types• Incremental query definition is easierPlease refer to the QueryDSL documentation which describes how to bootstrap your environment forAPT based code generation using Maven or using Ant.Using QueryDSL you will be able to write queries as shown belowQPerson person = new QPerson("person");List result = repository.findAll(person.address.zipCode.eq("C0123"));Page page = repository.findAll(person.lastname.contains("a"),new PageRequest(0, 2, Direction.ASC, "lastname"));QPerson is a class that is generated (via the Java annotation post processing tool) which is aPredicate that allows you to write type safe queries. Notice that there are no strings in the query otherthan the value "C0123".You can use the generated Predicate class via the interface QueryDslPredicateExecutor whichis shown belowpublic interface QueryDslPredicateExecutor {T findOne(Predicate predicate);List findAll(Predicate predicate);List findAll(Predicate predicate, OrderSpecifier... orders);Page findAll(Predicate predicate, Pageable pageable);}Long count(Predicate predicate);To use this in your repository implementation, simply inherit from it in additiion to other repositoryinterfaces. This is shown belowpublic interface PersonRepository extends MongoRepository,QueryDslPredicateExecutor {// additional finder methods go here}1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 75

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!