Book Image

EJB 3 Developer Guide

By : Michael Sikora
Book Image

EJB 3 Developer Guide

By: Michael Sikora

Overview of this book

Table of Contents (18 chapters)
EJB 3 Developer Guide
Credits
About the Author
About the Reviewers
Preface
Annotations and Their Corresponding Packages

Simple Queries


All examples in this chapter use the Bank Account scenario described in Chapter 4, with Customer, Address, Referee, and Account persisted entities.

To get all instances of the Customer entity type one can use the query:

SELECT c FROM Customer c

The c in the above query is referred to in JPQL as an identification variable. The type returned by a JPQL query depends on the expressions making up the query. In our example the return type is a list of Customer entities. This differs from JDBC where the return type is always wrapped in a ResultSet.

The above query can be created dynamically at runtime by supplying the query as a string argument using the EntityManager.createQuery() method. This returns a javax.persistence.Query instance:

Query query = em.createQuery("SELECT c FROM Customer c");

The alternative to dynamically specifying queries is to use named queries, we shall see examples of these later in this chapter. As our query returns a list, we would use the Query.getResultList...