The Java EE 6 Tutorial, Volume I

Simple Queries

If you are unfamiliar with the query language, these simple queries are a good place to start.

A Basic Select Query

FROM Player p

Data retrieved: All players.

Description: The FROM clause declares an identification variable named p, omitting the optional keyword AS. If the AS keyword were included, the clause would be written as follows:

FROM Player AS

The Player element is the abstract schema name of the Player entity.

See also: Identification Variables

Eliminating Duplicate Values

FROM Player p
WHERE p.position = ?1

Data retrieved: The players with the position specified by the query’s parameter.

Description: The DISTINCT keyword eliminates duplicate values.

The WHERE clause restricts the players retrieved by checking their position, a persistent field of the Player entity. The ?1 element denotes the input parameter of the query.

See also: Input Parameters, The DISTINCT Keyword

Using Named Parameters

FROM Player p
WHERE p.position = :position AND = :name

Data retrieved: The players having the specified positions and names.

Description: The position and name elements are persistent fields of the Player entity. The WHERE clause compares the values of these fields with the named parameters of the query, set using the Query.setNamedParameter method. The query language denotes a named input parameter using colon (:) followed by an identifier. The first input parameter is :position, the second is :name.