How to configure querydsl with Maven in Intellij IDE

Shyam Ramath
3 min readDec 26, 2019

--

Alittle bit of explanation about Querydsl, HQL for Hibernate was the first target language for Querydsl, but nowadays it supports JPA, JDO, JDBC, MongoDB, Lucene, Hibernate Search, Collections .

After reading this article you will be able to configure querydsl in a spring-boot maven project using an intellij IDE. Follow the step by step description in this article for the configuration.

Step 1

Create a spring-boot application using spring initilizer https://start.spring.io/ . After creating the maven based spring-boot project, import the project in to an IDE (you can use STS or Intellij, we are using intellij here)

Spring boot project imported in intellij

Step 2

Open the pom.xml from the project and add the below dependencies.Use a property to define the version, it is a good practice to keep it separated will help to update the version

<querydsl.version>4.2.2</querydsl.version><dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-collections</artifactId>
<version>${querydsl.version}</version>
</dependency>

Now add the plugin your pom.xml, the plugin is reposible for generating the Querydsl classes we are looking for. This plugin will generate the required classs during the maven build process, all the output of this process will be saved under the target folder, also the output path is configurable using the outputDirectory tag.

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

Step 3

Now lets us see how this works.

Create a simple Java POJO with query configuration, add a class level annotation @QueryEntity this will qualify your class .

import com.querydsl.core.annotations.QueryEntity;
import org.springframework.data.annotation.Id;

import java.io.Serializable;

@QueryEntity
public class City implements Serializable {

@Id
private String name;
private String country;

public String getName() {
return name;
}

public City setName(String name) {
this.name = name;
return this;
}

public String getCountry() {
return country;
}

public City setCountry(String country) {
this.country = country;
return this;
}

}

Now lets generate the classes, we need to build the project using maven commands. Run the below command from command line or run from your IDE.

mvn clean install

Step 4

The maven clean build should have generated all the required classes to support your code. Let’s verify that, open the target folder in the root of the project and expand the folder generated-sources. The class we are looking for will be generated by the querydsl maven plugin .

QCity class got generated during the maven build

Now you can use the generated class in the code, I have configured a data repository for my entity class.

Below is my data repository

import com.escuela.demo.hazlecast.cache.model.City;
import org.springframework.data.hazelcast.repository.HazelcastRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

public interface CityRepository extends HazelcastRepository <City, String>, QuerydslPredicateExecutor <City> {

}

Autowire the data repository with implementation class and then execute your query . Take a look at the findAll() method, the generated QCity class is used .

@Autowired
CityRepository cityRepository;
cityRepository.findAll(QCity.city.country.eq("US"), QCity.city.name.asc())

Refernce link

Follow the below link if you want to read more about Querydsl configuration

--

--