Home QuerydslPredicate with spring-boot-starter-jpa
Reply: 1

QuerydslPredicate with spring-boot-starter-jpa

rdabrowski
1#
rdabrowski Published in 2017-02-13 07:00:31Z

I have a problem with autogenerating Q classess in com.querydsl. I know that there are possible mistakes with compatibility between com.query.dsl and spring-boot-jpa but i tried lots of version without any positive result. Here is my pom.xml

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</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-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1212.jre7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <version>1.4.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>21.0</version>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.3</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>4.0.7</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.3</version>
            <executions>
                <execution>
                    <id>process-common-model</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources/apt</outputDirectory>
                        <processors>
                            <processor>com.query.apt.jpa.JPAAnnotationProcessor</processor>
                        </processors>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.querydsl</groupId>
                    <artifactId>querydsl-apt</artifactId>
                    <version>4.0.7</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

The error

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.jpa.repository.support.QueryDslJpaRepository]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class com.coig.prek.webservice.modules.quiz.entiry.QQuiz for domain class com.coig.prek.webservice.modules.quiz.entiry.Quiz! at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepositoryViaReflection(RepositoryFactorySupport.java:365) ~[spring-data-commons-1.13.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:101) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199) ~[spring-data-commons-1.13.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.0.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] ... 47 common frames omitted Caused by: java.lang.IllegalArgumentException: Did not find a query class com.coig.prek.webservice.modules.quiz.entiry.QQuiz for domain class com.coig.prek.webservice.modules.quiz.entiry.Quiz! at org.springframework.data.querydsl.SimpleEntityPathResolver.createPath(SimpleEntityPathResolver.java:63) ~[spring-data-commons-1.13.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.QueryDslJpaRepository.(QueryDslJpaRepository.java:84) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.QueryDslJpaRepository.(QueryDslJpaRepository.java:68) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111] at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111] at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] ... 56 common frames omitted Caused by: java.lang.ClassNotFoundException: com.coig.prek.webservice.modules.quiz.entiry.QQuiz at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_111] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_111] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_111] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_111] at org.springframework.util.ClassUtils.forName(ClassUtils.java:250) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE] at org.springframework.data.querydsl.SimpleEntityPathResolver.createPath(SimpleEntityPathResolver.java:53) ~[spring-data-commons-1.13.0.RELEASE.jar:na] ... 63 common frames omitted

Repository:

@Repository
public interface QuizRepository extends PagingAndSortingRepository<Quiz, Long>, QueryDslPredicateExecutor<Quiz> {
}

And Entity:

@javax.persistence.Entity
@Table(name = "QUIZ", uniqueConstraints = {
        @UniqueConstraint(columnNames = "NAME")
})
@ToString
public class Quiz extends Entity{

    @Column(name = "NAME")
    private @Setter @Getter String name;
    @Column(name = "TYPE")
    private @Setter @Getter Integer type;
    @Column(name = "TIME")
    private @Setter @Getter Long time;
    @JsonProperty(value = "branch")
    @Column(name = "BRANCH")
    private @Setter @Getter Integer branch;

    @OneToMany
    private @Setter @Getter Set<Question> questions;

    public Quiz() {}

}

Can you tell me if I missing something, or what I am doing wrong? Thanks for any help.

jcgarcia
2#
jcgarcia Reply to 2017-02-13 07:50:49Z

Here's an example about how to use QueryDSL:

Include the following dependencies in your pom.xml file:

    <dependency>
        <groupId>io.springlets</groupId>
        <artifactId>springlets-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
    </dependency>

Also, include the following plugins to be able to generate the Q classes:

            <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>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.querydsl</groupId>
                        <artifactId>querydsl-apt</artifactId>
                        <version>${querydsl.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>com.querydsl</groupId>
                        <artifactId>querydsl-jpa</artifactId>
                        <version>${querydsl.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

Now, creates a new repository interface called QuizRepositoryCustom and define the methods you want to provide.

public interface QuizRepositoryCustom {

  // SAMPLE METHOD TO SEARCH QUIZ BY NAME
  public Page<Quiz> findByNameLike(String name, Pageable pageable);

}

Creates the implementation of the QuizRepositoryCustom. This class should extends QueryDslRepositorySupport

public class QuizRepositoryImpl extends QueryDslRepositorySupport<Quiz> implements QuizRepositoryCustom {

    QuizRepositoryImpl() {
        super(Quiz.class);
    }

    public Page<Quiz> findByNameLike(String name, Pageable pageable) {

        QQuiz quiz = QQuiz.quiz;

        JPQLQuery<Quiz> query = from(quiz);

        if (StringUtils.isNotEmpty(name)) {
           BooleanBuilder searchCondition = new BooleanBuilder();                         

           searchCondition.and(quiz.name.eq(name));

           if (searchCondition.hasValue()) {
            query.where(searchCondition);
           }
        }

        long totalFound = query.fetchCount();
        List<Quiz> results = query.select(quiz).fetch();
        return new PageImpl<M>(results, pageable, totalFound);
    }

}

Now, extend the QuizRepository interface from JpaRepository<Quiz, Long> and QuizRepositoryCustom interface.

public interface QuizRepository extends QuizRepositoryCustom, JpaRepository<Quiz, Long> {

}

With that simple steps, you will be able to use QueryDSL.

Also, you could check the following project that contains more examples about how to use QueryDSL.

https://github.com/jcagarcia/proofs/tree/master/spring-security-and-formatters/src/main/java/org/springframework/roo/petclinic/repository

Hope it helps,

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298799 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO