Instead of a general Getting started guide we provide integration guides for the main backends of Querydsl.
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.
Querydsl for JPA 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.
Add the following dependencies to your Maven project:
<dependency> <groupId>com.mysema.querydsl</groupId> <artifactId>querydsl-apt</artifactId> <version>${querydsl.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.mysema.querydsl</groupId> <artifactId>querydsl-jpa</artifactId> <version>${querydsl.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency>
And now, configure the Maven APT plugin:
<project> <build> <plugins> ... <plugin> <groupId>com.mysema.maven</groupId> <artifactId>apt-maven-plugin</artifactId> <version>1.0.9</version> <executions> <execution> <goals> <goal>process</goal> </goals> <configuration> <outputDirectory>target/generated-sources/java</outputDirectory> <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor> </configuration> </execution> </executions> </plugin> ... </plugins> </build> </project>
The JPAAnnotationProcessor finds domain types annotated with the javax.persistence.Entity annotation and generates query types for them.
If you use Hibernate annotations in your domain types you should use
the APT processor
com.mysema.query.apt.hibernate.HibernateAnnotationProcessor
instead.
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 JPA query instances and instances of the query domain model.
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.jpa.JPAAnnotationProcessor"/> <compilerarg value="-s"/> <compilerarg value="${generated}"/> </javac> <!-- compilation --> <javac classpathref="cp" destdir="${build}"> <src path="${src}"/> <src path="${generated}"/> </javac>
Replace src with your main source folder, generated with your folder for generated sources and build with your target folder.
If you are using Querydsl JPA with Spring Roo you can replace
com.mysema.query.apt.jpa.JPAAnnotationProcessor
with
com.mysema.query.apt.roo.RooAnnotationProcessor
which will handle
@RooJpaEntity
and @RooJpaActiveRecord
annotated classes instead of
@Entity
annotated classes.
APT based code generation doesn't work well with AspectJ IDTs.
If you are using Hibernate with an XML based configuration, you can use the XML metadata to create your Querydsl model.
com.mysema.query.jpa.codegen.HibernateDomainExporter
provides the
functionality for this:
HibernateDomainExporter exporter = new HibernateDomainExporter( "Q", // name prefix new File("target/gen3"), // target folder configuration); // instance of org.hibernate.cfg.Configuration exporter.export();
The HibernateDomainExporter needs to be executed within a classpath where the domain types are visible, since the property types are resolved via reflection.
All JPA annotations are ignored, but Querydsl annotations such as @QueryInit and @QueryType are taken into account.
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:
@Entity 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 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");
The Querydsl JPA module supports both the JPA and the Hibernate API.
To use the JPA API you use JPAQuery
instances for your queries like
this:
// where entityManager is a JPA EntityManager JPAQuery query = new JPAQuery(entityManager);
If you are using the Hibernate API instead, you can instantiate a
HibernateQuery
like this:
// where session is a Hibernate session HibernateQuery query = new HibernateQuery(session);
Both JPAQuery and HibernateQuery implement the JPQLQuery interface.
To retrieve the customer with the first name Bob you would construct a query like this:
QCustomer customer = QCustomer.customer; JPAQuery query = new JPAQuery(entityManager); Customer bob = query.from(customer) .where(customer.firstName.eq("Bob")) .uniqueResult(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 use the query like this:
QCustomer customer = QCustomer.customer; QCompany company = QCompany.company; query.from(customer, company);
And to use multiple filters use it like this
query.from(customer) .where(customer.firstName.eq("Bob"), customer.lastName.eq("Wilson"));
Or like this
query.from(customer) .where(customer.firstName.eq("Bob").and(customer.lastName.eq("Wilson")));
In native JPQL form the query would be written like this:
from Customer as customer where customer.firstName = "Bob" and customer.lastName = "Wilson"
If you want to combine the filters via "or" then use the following pattern
query.from(customer) .where(customer.firstName.eq("Bob").or(customer.lastName.eq("Wilson")));
Querydsl supports the following join variants in JPQL: inner join, join, left join and full join. Join usage is typesafe, and follows the following pattern:
QCat cat = QCat.cat; QCat mate = new QCat("mate"); QCate kitten = new QCat("kitten"); query.from(cat) .innerJoin(cat.mate, mate) .leftJoin(cat.kittens, kitten) .list(cat);
The native JPQL version of the query would be
from Cat as cat inner join cat.mate as mate left outer join cat.kittens as kitten
Another example
query.from(cat)
.leftJoin(cat.kittens, kitten)
.on(kitten.bodyWeight.gt(10.0))
.list(cat);
With the following JPQL version
from Cat as cat left join cat.kittens as kitten on kitten.bodyWeight > 10.0
Use the the cascading methods of the JPQLQuery interface like this
from: Add the query sources here.
innerJoin, join, leftJoin, fullJoin, on: Add join elements using these constructs. For the join methods the first argument is the join source and the second the target (alias).
where: Add query filters, either in varargs form separated via commas or cascaded via the and-operator.
groupBy: Add group by arguments in varargs form.
having: Add having filters of the "group by" grouping as an varags array of Predicate expressions.
orderBy: Add 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: Set the paging of the result. Limit for max results, offset for skipping rows and restrict for defining both in one call.
The syntax for declaring ordering is
QCustomer customer = QCustomer.customer; query.from(customer) .orderBy(customer.lastName.asc(), customer.firstName.desc()) .list(customer);
which is equivalent to the following native JPQL
from Customer as customer order by customer.lastName asc, customer.firstName desc
Grouping can be done in the following form
query.from(customer) .groupBy(customer.lastName) .list(customer.lastName);
which is equivalent to the following native JPQL
select customer.lastName from Customer as customer group by customer.lastName
Delete clauses in Querydsl JPA follow a simple delete-where-execute form. Here are some examples:
QCustomer customer = QCustomer.customer; // delete all customers new JPADeleteClause(entityManager, customer).execute(); // delete all customers with a level less than 3 new JPADeleteClause(entityManager, customer).where(customer.level.lt(3)).execute();
The second parameter of the JPADeleteClause 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.
For Hibernate based Delete usage, use the HibernateDeleteClause instead.
DML clauses in JPA don't take JPA level cascade rules into account and don't provide finegrained second level cache interaction.
Update clauses in Querydsl JPA follow a simple update-set/where-execute form. Here are some examples:
QCustomer customer = QCustomer.customer; // rename customers named Bob to Bobby new JPAUpdateClause(session, customer).where(customer.name.eq("Bob")) .set(customer.name, "Bobby") .execute();
The second parameter of the JPAUpdateClause 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 Hibernate based Update usage, use the HibernateUpdateClause instead.
DML clauses in JPA don't take JPA level cascade rules into account and don't provide finegrained second level cache interaction.
To create a subquery you create a JPASubQuery 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.
QDepartment department = QDepartment.department; QDepartment d = new QDepartment("d"); query.from(department) .where(department.employees.size().eq( new JPASubQuery().from(d).unique(d.employees.size().max()) )).list(department);
Another example
QEmployee employee = QEmployee.employee; QEmployee e = new QEmployee("e"); query.from(employee) .where(employee.weeklyhours.gt( new JPASubQuery().from(employee.department.employees, e) .where(e.manager.eq(employee.manager)) .unique(e.weeklyhours.avg()) )).list(employee);
For Hibernate based sub query usage, use the HibernateSubQuery instead.
If you need to tune the original Query before the execution of the query you can expose it like this:
JPAQuery query = new JPAQuery(entityManager); Query jpaQuery = query.from(employee).createQuery(employee); // ... List results = jpaQuery.getResultList();
Querydsl supports Native SQL in JPA via the JPASQLQuery 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:
<plugin> <groupId>com.mysema.querydsl</groupId> <artifactId>querydsl-maven-plugin</artifactId> <version>${project.version}</version> <executions> <execution> <goals> <goal>export</goal> </goals> </execution> </executions> <configuration> <jdbcDriver>org.apache.derby.jdbc.EmbeddedDriver</jdbcDriver> <jdbcUrl>jdbc:derby:target/demoDB;create=true</jdbcUrl> <packageName>com.mycompany.mydomain</packageName> <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder> </configuration> <dependencies> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <version>${derby.version}</version> </dependency> </dependencies> </plugin>
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"); QCat catEntity = QCat.cat; JPASQLQuery query = new JPASQLQuery(entityManager, templates); List<String> names = query.from(cat).list(cat.name);
If you mix entity (e.g. QCat) and table (e.g. SAnimal) references in your query you need to make sure that they use the same variable names. SAnimal.animal has the variable name "animal", so a new instance (new SAnimal("cat")) was used instead.
An alternative pattern could be
QCat catEntity = QCat.cat;
SAnimal cat = new SAnimal(catEntity.getMetadata().getName());
Query multiple columns:
query = new JPASQLQuery(entityManager, 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 in SQL, but project as entity:
query = new JPASQLQuery(entityManager, templates);
List<Cat> cats = query.from(cat).orderBy(cat.name.asc()).list(catEntity);
Query with joins:
query = new JPASQLQuery(entityManager, templates); cats = query.from(cat) .innerJoin(mate).on(cat.mateId.eq(mate.id)) .where(cat.dtype.eq("Cat"), mate.dtype.eq("Cat")) .list(catEntity);
Query and project into DTO:
query = new JPASQLQuery(entityManager, templates); List<CatDTO> catDTOs = query.from(cat) .orderBy(cat.name.asc()) .list(ConstructorExpression.create(CatDTO.class, cat.id, cat.name));
If you are using the Hibernate API instead of the JPA API, then use HibernateSQLQuery instead.