2. Tutorials

Instead of a general Getting started guide we provide integration guides for the main backends of Querydsl.

2.1. Querying JDO

Querydsl defines a general statically typed syntax for querying on top of persisted domain model data. JDO and JPA are the primary integration technologies for Querydsl. This guide describes how to use Querydsl in combination with JDO.

2.1.1. Maven integration

Add the following dependencies to your Maven project and make sure that the Maven 2 repo of Mysema Source (http://source.mysema.com/maven2/releases) is accessible from your POM if the version cannot yet be found in other public Maven repos :



And now, configure the Maven APT plugin which generates the query types used by Querydsl :


The JDOAnnotationProcessor finds domain types annotated with the javax.jdo.annotations.PersistenceCapable annotation and generates Querydsl query types for them.

Run clean install and you will get your Query types generated into target/generated-sources/java.

If you use Eclipse, run mvn eclipse:eclipse to update your Eclipse project to include target/generated-sources/java as a source folder.

Now you are able to construct JDOQL query instances and instances of the query domain model.

2.1.2. Ant integration

Place the jar files from the full-deps bundle on your classpath and use the following tasks for Querydsl code generation :

    <!-- APT based code generation -->
    <javac srcdir="${src}" classpathref="cp">
      <compilerarg value="-proc:only"/>      
      <compilerarg value="-processor"/>
      <compilerarg value="com.mysema.query.apt.jdo.JDOAnnotationProcessor"/>
      <compilerarg value="-s"/>
      <compilerarg value="${generated}"/>
    <!-- compilation -->
    <javac classpathref="cp" destdir="${build}">      
      <src path="${src}"/>
      <src path="${generated}"/>

Replace src with your main source folder, generated with your folder for generated sources and build with your target folder.

2.1.3. Using query types

To create queries with Querydsl you need to instantiate variables and Query implementations. We will start with the variables.

Let's assume that your project has the following domain type :

public class Customer {
  private String firstName;
  private String lastName;
  public String getFirstName(){
    return firstName;
  public String getLastName(){
    return lastName;
  public void setFirstName(String fn){
    firstName = fn;
  public void setLastName(String ln)[
    lastName = ln;

Querydsl will generate a query type with the simple name QCustomer into the same package as Customer. QCustomer can be used as a statically typed variable in Querydsl as a representative for the Customer type.

QCustomer has a default instance variable which can be accessed as a static field :

QCustomer customer = QCustomer.customer;

Alternatively you can define your own Customer variables like this :

QCustomer customer = new QCustomer("myCustomer");

QCustomer reflects all the properties of the original type Customer as public fields. The firstName field can be accessed like this


2.1.4. Querying with JDOQL

For the JDOQL-module JDOQLQueryImpl is the main Query implementation. It is instantiated like this :

PersistenceManager pm = ...;
JDOQLQuery query = new JDOQLQueryImpl (pm);

To retrieve the customer with the first name Bob you would construct a query like this :

QCustomer customer = QCustomer.customer;
JDOQLQuery query = new JDOQLQueryImpl (pm);
Customer bob = query.from(customer)

The from call defines the query source, the where part defines the filter and uniqueResult defines the projection and tells Querydsl to return a single element. Easy, right?

To create a query with multiple sources you just use the JDOQLQuery interface like this :

query.from(customer, company);    

And to use multiple filters use it like this

    .where(customer.firstName.eq("Bob"), customer.lastName.eq("Wilson"));   

Or like this


2.1.5. General usage

Use the the cascading methods of the JDOQLQuery interface like this

from : Define the query sources here, the first argument becomes the main source and the others are treated as variables.

where : Define the query filters, either in varargs form separated via commas or cascaded via the and-operator.

groupBy : Define the group by arguments in varargs form.

having : Define the having filter of the "group by" grouping as an varargs array of Predicate expressions.

orderBy : Define the ordering of the result as an varargs array of order expressions. Use asc() and desc() on numeric, string and other comparable expression to access the OrderSpecifier instances.

limit, offset, restrict : Define the paging of the result. Limit for max results, offset for skipping rows and restrict for defining both in one call.

2.1.6. Ordering

The syntax for declaring ordering is

    .orderBy(customer.lastName.asc(), customer.firstName.desc())

2.1.7. Grouping

Grouping can be done in the following form


2.1.8. Delete clauses

Delete clauses in Querydsl JDOQL follow a simple delete-where-execute form. Here are some examples :

QCustomer customer = QCustomer.customer;
// delete all customers
new JDOQLDeleteClause(pm, customer).execute();
// delete all customers with a level less than 3
new JDOQLDeleteClause(pm, customer).where(customer.level.lt(3)).execute();  

The second parameter of the JDOQLDeleteClause constructor is the entity to be deleted. The where call is optional and the execute call performs the deletion and returns the amount of deleted entities.

2.1.9. Subqueries

To create a subquery you create a JDOQLSubQuery instance, define the query parameters via from, where etc and use unique or list to create a subquery, which is just a type-safe Querydsl expression for the query. unique is used for a unique result and list for a list result.

        new JDOQLSubQuery().from(d).unique(AggregationFunctions.max(d.employees.size()))

represents the following native JDOQL query

SELECT this FROM com.mysema.query.jdoql.models.company.Department
WHERE this.employees.size() == 
(SELECT max(d.employees.size()) FROM com.mysema.query.jdoql.models.company.Department d)

Another example

        new JDOQLSubQuery().from(employee.department.employees, e)

which represents the following native JDOQL query

SELECT this FROM com.mysema.query.jdoql.models.company.Employee 
WHERE this.weeklyhours > 
(SELECT avg(e.weeklyhours) FROM this.department.employees e WHERE e.manager == this.manager)

2.1.10. Using Native SQL

Querydsl supports Native SQL in JDO via the JDOSQLQuery class.

To use it, you must generate Querydsl query types for your SQL schema. This can be done for example with the following Maven configuration :


When the query types have successfully been generated into the location of your choice, you can use them in your queries.

Single column query :

// serialization templates
SQLTemplates templates = new DerbyTemplates(); 
// query types (S* for SQL, Q* for domain types)
SAnimal cat = new SAnimal("cat");   
SAnimal mate = new SAnimal("mate"); 
JDOSQLQuery query = new JDOSQLQuery(pm, templates);
List<String> names = query.from(cat).list(cat.name);

Query multiple columns :

query = new JDOSQLQuery(pm, templates);
List<Object[]> rows = query.from(cat).list(cat.id, cat.name);

Query all columns :

List<Object[]> rows = query.from(cat).list(cat.all());

Query with joins :

query = new JDOSQLQuery(pm, templates);
cats = query.from(cat)
    .where(cat.dtype.eq("Cat"), mate.dtype.eq("Cat"))

Query and project into DTO :

query = new JDOSQLQuery(pm, templates);
List<CatDTO> catDTOs = query.from(cat)
    .list(EConstructor.create(CatDTO.class, cat.id, cat.name));