Spring Data MongoDB Repository - JPA Specifications like
Asked Answered
K

2

19

Is there something like JPA Specifications for Spring Data MongoDB Repositories?

If not, how can I make dynamic queries with repositories?

A classic scenario could be a search form with optional fields that the user will fill.

Kenny answered 16/11, 2015 at 12:48 Comment(0)
K
15

I found myself a way.

The trick can be done using QueryDSL, in the following way:

First, add the QueryDSL dependencies:

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-mongodb</artifactId>
    <version>${querydsl-mongo.version}</version>
</dependency>

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>${querydsl-mongo.version}</version>
</dependency>

Then, configure the plugin in order to create Metamodels classes (their names will be the same of the documents with the letter Q as prefix: eg. QUser):

    <build>
        <plugins>
            ....
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Note the processor class: it's not the QueryDSL default one com.mysema.query.apt.morphia.MorphiaAnnotationProcessor, but the Spring Data MongoDB one org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor:

Spring Data Mongo provides a custom APT processor to generate the Metamodels instead of the one provided in QueryDSL, it will scan the Spring specific @Document instead of the Morphia specific annotations.

Now we can make our repository interface extending QueryDslPredicateExecutor<T>:

public interface UserRepository extends MongoRepository<User, String>, QueryDslPredicateExecutor<User>,
        QuerydslBinderCustomizer<QUser> {
}

We can now define Predicates when querying the repository:

QUser user = QUser.user;            
BooleanExpression predicate = user.name.containsIgnoreCase("John");         
userRepository.findAll(predicate);

QuerydslBinderCustomizer helps you to define the binding of the Document's properties (see Spring documentation for further help).

Kenny answered 20/11, 2015 at 16:12 Comment(1)
For an implementation of this and also for the reactive version, there is an example: github.com/spring-projects/spring-data-examples/tree/master/…Herbie
R
1

This is update as of:

<spring.version>4.3.4.RELEASE</spring.version>
<querydsl.version>4.1.3</querydsl.version>
<spring.data.mongodb.version>1.9.5</spring.data.mongodb.version>

use

...
            <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources/java</outputDirectory>
                        <processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
...

    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-mongodb</artifactId>
        <version>${querydsl.version}</version>
    </dependency>

    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>${querydsl.version}</version>
    </dependency>
Redintegration answered 30/11, 2016 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.