Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource Spring Data MongoDB - Spring Web Services - Parent - SpringSource

static.springsource.org
from static.springsource.org More from this publisher
13.07.2015 Views

please define productname in your docbook file!Type-safe Query methodsMongoDB 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-SNAPSHOTSpring Data MongoDB -Reference Documentation 75

please define productname in your docbook file!We think you will find this an extremely powerful tool for writing MongoDB queries.6.4 MiscellaneousCDI IntegrationInstances of the repository interfaces are usually created by a container, which Spring is the most naturalchoice when working with Spring Data. As of version 1.3.0 Spring Data MongoDB ships with a customCDI extension that allows using the repository abstraction in CDI environments. The extension is part ofthe JAR so all you need to do to activate it is dropping the Spring Data MongoDB JAR into your classpath.You can now set up the infrastructure by implementing a CDI Producer for the MongoTemplate:class MongoTemplateProducer {@Produces@ApplicationScopedpublic MongoOperations createMongoTemplate() throws UnknownHostException,MongoException {}}MongoDbFactory factory = new SimpleMongoDbFactory(new Mongo(), "database");return new MongoTemplate(factory);The Spring Data MongoDB CDI extension will pick up the MongoTemplate available as CDI bean andcreate a proxy for a Spring Data repository whenever an bean of a repository type is requested by thecontainer. Thus obtaining an instance of a Spring Data repository is a matter of declaring an @Injectedproperty:class RepositoryClient {@InjectPersonRepository repository;public void businessMethod() {}}List people = repository.findAll();1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 76

please define productname in your docbook file!We think you will find this an extremely powerful tool for writing <strong>MongoDB</strong> queries.6.4 MiscellaneousCDI IntegrationInstances of the repository interfaces are usually created by a container, which <strong>Spring</strong> is the most naturalchoice when working with <strong>Spring</strong> <strong>Data</strong>. As of version 1.3.0 <strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> ships with a customCDI extension that allows using the repository abstraction in CDI environments. The extension is part ofthe JAR so all you need to do to activate it is dropping the <strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> JAR into your classpath.You can now set up the infrastructure by implementing a CDI Producer for the MongoTemplate:class MongoTemplateProducer {@Produces@ApplicationScopedpublic MongoOperations createMongoTemplate() throws UnknownHostException,MongoException {}}MongoDbFactory factory = new SimpleMongoDbFactory(new Mongo(), "database");return new MongoTemplate(factory);The <strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> CDI extension will pick up the MongoTemplate available as CDI bean andcreate a proxy for a <strong>Spring</strong> <strong>Data</strong> repository whenever an bean of a repository type is requested by thecontainer. Thus obtaining an instance of a <strong>Spring</strong> <strong>Data</strong> repository is a matter of declaring an @Injectedproperty:class RepositoryClient {@InjectPersonRepository repository;public void businessMethod() {}}List people = repository.findAll();1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 76

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

Saved successfully!

Ooh no, something went wrong!