2.2. Querying JPA

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 JPA/Hibernate.

Querydsl for JPA/Hibernate is an alternative to both JPQL and Criteria queries. It combines the dynamic nature
of Criteria queries with the expressiveness of JPQL and all that in a fully typesafe manner.

2.2.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 :

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 queries 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");

2.2.5. Querying

The Querydsl JPA module supports both the JPA and the Hibernate API.

To use the Hibernate API you use HibernateQuery instances for your queries like this :

// where session is a Hibernate session
JPQLQuery query = new HibernateQuery (session);

If you are using the JPA API instead, you can instantiate a JPAQuery like this :

// where entityManager is a JPA EntityManager
JPQLQuery query = new JPAQuery (entityManager);

Both HibernateQuery and JPAQuery implement the JPQLQuery interface.

The default configuration of JPAQuery is best suited for use with Hibernate. Whe using EclipseLink as the JPA provider, the
JPAQuery should be constructed like this :

The second parameter of the HibernateDeleteClause 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.

The second parameter of the HibernateUpdateClause constructor is the entity to be updated.
The set invocations define the property updates in SQL-Update-style and the execute call performs
the Update and returns the amount of updated entities.

For JPA based Update usage, use the JPAUpdateClause instead.

2.2.12. Subqueries

To create a subquery you create a HibernateSubQuery 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 (single) result and list for a list result.