Using parameters in a query

If a query could not use parameters in the same manner as methods, they would have limited utility. JPQL supports two types of query parameters: named and positional. This recipe illustrates their use and how they can enhance the utility of a query. The use of this technique will improve your ability to use JPQL in an efficient and reusable manner.

Getting ready

Named and positional parameters are used as part of the query string and are assigned values using the setParameter method. We use parameters in a query by:

  1. Creating a query using either a named or positional parameter
  2. Using the setParameter method to assign a value to the parameter
  3. Executing the query

    Named parameters are prefixed with a : and are embedded as part ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.