Codevalley
Codevalley

Reputation: 4651

First time MongoDB with QueryDSL on Spring

I am trying to simple MongoDB querying with QueryDSL. I am just using Spring Data to access MongoDB right now. I am not able to create Q Classes whatever I try.

Here is what I have tried so far.

issue.java

@QueryEntity
@Document(collection = "issues")
public class Issue {

    private @Id long id;

    private String body;
    private long prodId;
    private long timestamp;
    public Issue() {}
    ...

IssueRepository.java

@RepositoryRestResource(collectionResourceRel = "issues", path = "issues")
public interface IssueRepository extends MongoRepository<IssueModel, String>,
QueryDslPredicateExecutor<IssueModel>{

    public List<IssueModel> findByBody(String body);
    public List<IssueModel> findByProdId(long prodId);

}

Pom.xml

<dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.10.12</version>
            <exclusions>
                <exclusion>
                    <artifactId>jackson-core</artifactId>
                    <groupId>com.fasterxml.jackson.core</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jackson-databind</artifactId>
                    <groupId>com.fasterxml.jackson.core</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jackson-annotations</artifactId>
                    <groupId>com.fasterxml.jackson.core</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> 
            </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-remote-shell</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <exclusions>
                <exclusion>
                    <artifactId>tools</artifactId>
                    <groupId>com.sun</groupId>
                </exclusion>
            </exclusions>
            <groupId>com.basho.riak</groupId>
            <artifactId>riak-client</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-utils</artifactId>
            <version>3.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>net.kencochrane.raven</groupId>
            <artifactId>raven-logback</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>3.6.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-mongodb</artifactId>
            <version>3.6.8</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                    <phase>generate-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/gen-sources/java</outputDirectory>
                            <processor>com.mysema.query.apt.QuerydslAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

When I run maven build-sources nothing happens. When I change POM.xml to use com.mysema.query.apt.morphia.MorphiaAnnotationProcessor instead, I get classnotfound exception for Entity.

I can't find one simple tutorial on the internet around this.

Upvotes: 3

Views: 4740

Answers (2)

Rahim
Rahim

Reputation: 740

You can add this small library https://github.com/rahimalizada/spring-data-mongodb-querydsl-processor I created specifically to enable automated annotation processing for QueryDSL and spring-data-mongo using maven-compiler-plugin. It also works correctly when performing IDE (IntelliJ) builds.

Sample usage:

 <build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.11.0</version>
      <configuration>
        <annotationProcessorPaths>
          <path>
            <groupId>com.jyvee</groupId>
            <artifactId>spring-data-mongodb-querydsl-processor</artifactId>
            <version>1.0.0</version>
          </path>
          <path>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>5.0.0</version>
            <classifier>general</classifier>
          </path>
          <path>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>4.1.4</version>
          </path>
        </annotationProcessorPaths>
      </configuration>
    </plugin>
  </plugins>
</build>

Upvotes: 0

s1moner3d
s1moner3d

Reputation: 1280

Change the plugin processor to 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.

Then run mvn compile to generate the Metamodels in the outputDirectory you specified.

You can also remove the @QueryEntity annotation.

Upvotes: 6

Related Questions