SELECT Clause

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 variable> | <aggregation expression> ]

The clause contains a single variable reference to indicate the type of result returned by the query, or an aggregation function can be used to calculate some formula on the results. 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

This 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 ...

Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.