Spring Data MongoDB - Spring Web Services - Parent - SpringSource
Spring Data MongoDB - Spring Web Services - Parent - SpringSource Spring Data MongoDB - Spring Web Services - Parent - SpringSource
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
- Page 30 and 31: please define productname in your d
- Page 32 and 33: please define productname in your d
- Page 34 and 35: please define productname in your d
- Page 36 and 37: please define productname in your d
- Page 38 and 39: please define productname in your d
- Page 40 and 41: please define productname in your d
- Page 42 and 43: please define productname in your d
- Page 44 and 45: please define productname in your d
- Page 46 and 47: please define productname in your d
- Page 48 and 49: please define productname in your d
- Page 50 and 51: please define productname in your d
- Page 52 and 53: please define productname in your d
- Page 54 and 55: please define productname in your d
- Page 56 and 57: please define productname in your d
- Page 58 and 59: please define productname in your d
- Page 60 and 61: please define productname in your d
- Page 62 and 63: please define productname in your d
- Page 64 and 65: please define productname in your d
- Page 66 and 67: please define productname in your d
- Page 68 and 69: please define productname in your d
- Page 70 and 71: please define productname in your d
- Page 72 and 73: please define productname in your d
- Page 74 and 75: please define productname in your d
- Page 76 and 77: please define productname in your d
- Page 78 and 79: please define productname in your d
- Page 82 and 83: please define productname in your d
- Page 84 and 85: please define productname in your d
- Page 86 and 87: please define productname in your d
- Page 88 and 89: please define productname in your d
- Page 90 and 91: please define productname in your d
- Page 92 and 93: please define productname in your d
- Page 94 and 95: please define productname in your d
- Page 96 and 97: please define productname in your d
- Page 98 and 99: please define productname in your d
- Page 100 and 101: please define productname in your d
- Page 102 and 103: please define productname in your d
- Page 104: please define productname in your d
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