Cambridge Technology PartnersCommunity Documentation

Chapter 1. Installation

1.1. Prerequisites
1.2. Maven dependency configuration
1.3. Setup your application for CDI Query

To use the CDI Query module, you need to add the API and implementation JARs to the runtime classpath of your enterprise application.

In order to use the CDI Query module, you have to run your application in a Java EE container supporting at least the Java EE 6 Web Profile. Other configurations like running it inside Tomcat or even a Java SE application are possible but currently not supported.

If you are using Maven as your build tool, you can add the following dependencies to your pom.xml file to include the CDI Query module:


<dependency>
    <groupId>com.ctp.cdi.query</groupId>
    <artifactId>cdi-query-api</artifactId>
    <version>${cdi.query.version}</version>
    <scope>compile</scope>
</dependency>

<dependency>
    <groupId>com.ctp.cdi.query</groupId>
    <artifactId>cdi-query-impl</artifactId>
    <version>${cdi.query.version}</version>
    <scope>runtime</scope>
</dependency>
        

Tip

Substitute the expression ${cdi.query.version} with the most recent or appropriate version of the CDI Query module. Alternatively, you can create a Maven user-defined property to satisfy this substitution so you can centrally manage the version.

Including the API at compile time and only include the implementation at runtime protects you from inadvertantly depending on an implementation class. Alternatively you can add the following single dependency to your pom.xml file to include the CDI Query module:


<dependency>
    <groupId>com.ctp.cdi.query</groupId>
    <artifactId>cdi-query</artifactId>
    <version>${cdi.query.version}</version>
</dependency>
        

CDI Query requires an EntityManager exposed via a CDI producer - which is common practice in Java EE 6 applications.

public class DataSourceProducer {


    @PersistenceUnit
    private EntityManagerFactory emf;
    @Produces
    public EntityManager create(){
        return emf.createEntityManager();
    }
    public void close(@Disposes EntityManager em) {
        em.close();
    }
}    

This allows the EntityManager to be injected over CDI instead of only being used with a @PersistenceContext annotation. Using multiple EntityManagers is explored in more detail in Section 2.2, “Using Multiple EntityManagers”

You're now ready to use the CDI Query module in your DAOs!