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.
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:
- Creating a query using either a named or positional parameter
- Using the
setParametermethod to assign a value to the parameter
- Executing the query
Named parameters are prefixed with a
:and are embedded as part ...