Cambridge Technology PartnersCommunity Documentation

Chapter 5. Extending CDI Query

5.1. Query Delegates
5.1.1. Implementing the Query Delegate
5.1.2. Using the DAO Extension
5.2. Including Querydsl

While CDI Query defines several base interfaces, there might still be one or another convenience method that is missing. This is actually intentional - things should not get overloaded for each and every use case. That's why in CDI Query you can define your own reusable methods.

For example, you might want to refresh an entity immediately after you've flushed it to the database (e.g. because a trigger fired and updated your entity). You might want to define the following method on a base DAO:

public E saveAndRefresh(E entity) {

    entityManager.persist(entity);
    entityManager.flush();
    entityManager.refresh(entity);
    return entity;
}    

This method is generic enough that you can use it in all your DAOs. You can now mixin this method in your DAOs with a simple mechanism.

CDI Query contains basic support for the Querydsl framework for JPA. Implementing the QueryDslSupport interface (and including the Querydsl dependencies) gives you access to a JPAQuery handle.

You can find more information about Querydsl on the website.