As with standard SQL queries, the SELECT clause in EJB QL can specify what you want returned from the persistent store. The SELECT clause takes the form:

SELECT [DISTINCT] <schema type>

The clause contains a single variable reference to indicate the type of result returned by the query. The optional DISTINCT operator specifies that the return results should be unique. The variable reference can either be to a specific variable declared in the FROM clause, or it can be a path expression that refers to a single-valued cmr-field on an abstract schema type. In our previous example, we used a specific variable reference:

SELECT OBJECT(p) FROM ProfileBean AS p WHERE p.entriesBytes IS NULL

The SELECT clause specifies OBJECT(p) as the return value of the query. If a single query variable is being used in the SELECT clause, then it has to be qualified with the OBJECT operator. The p variable is declared in the FROM clause as a range variable with type ProfileBean, so the query results are one or more ProfileBeans that match the conditions in the WHERE clause. In our other query example:

SELECT DISTINCT OBJECT(prof) FROM Person p, IN(p.profiles) prof WHERE ...

the SELECT clause specifies OBJECT(prof) as the return value of the query. The prof variable is declared in the FROM clause as a collection member variable that refers to the profiles cmr-field in the Person bean. This cmr-field contains EJBs with the abstract schema type of ProfileBean, so the query results again are ProfileBean ...

Get Java Enterprise in a Nutshell, Second Edition now with O’Reilly online learning.

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