3.4. Best practices

3.4.1. Use default variable of the Query types

Use the default variables of the query types as much as possible. The default variables are available as static final fields in the query types. The name is always the decapitalized version of the simple type name. For the type Account this would be account :

     
public class QAccount extends EntityPathBase<Account>{
      
    public static final QAccount account = new QAccount("account");
    
}

Querydsl query types are safe to re-use, and by using Querydsl default variables you save initialization time and memory.

3.4.2. DAO integration

A practice which we have found to be very easy to use is to provide factory methods for Query instances in DAO implementations in the following form.

For JPA usage :

 
protected JPAQuery from(EntityPath<?>... o) {
    return new JPAQuery(entityManager).from(o);
} 

For JDO usage :

protected JDOQLQuery from(EntityPath<?>... o) {
    return new JDOQLQueryImpl(persistenceManager).from(o);
}