Cambridge Technology PartnersCommunity Documentation

Introduction

The DAO pattern (Data Access Object) used to be one of the core J2EE patterns and could be found in most enterprise applications reading and writing data to persistent stores. While the Java Persistence API (JPA) as part of Java EE 5+ has replaced many aspects of the DAO pattern, it is still a good approach to centralize complex query logic related to specific entities.

The CDI Query module is intended to help you simplifying your DAO layer. While you will have complex queries in a DAO requiring your full attention, there will also be many simple ones often requiring repetitive code and cluttering your DAO. This is where the CDI query module will help you keeping your DAO lean so you can focus on the though things. The code sample below will give you a quick overview on the common usage scenarios of the CDI Query module:

public interface PersonDao extends EntityDao<Person, Long> {


    List<Person> findByAgeBetweenAndGender(int minAge, int maxAge, Gender gender);
    @Query("select p from Person p where p.ssn = ?1")
    Person findBySSN(String ssn);
    
    @Query(named=Person.BY_FULL_NAME)
    Person findByFullName(String firstName, String lastName);
}

As you see in the sample, there are several usage scenarios outlined here:

The implementation of the method is done automatically by the CDI Query module. A client can declare a dependency to the interface only. The details on how to use those features are outlines in the following chapters.