Java Reference
In-Depth Information
SELECT c.categoryName, c.createdBy
FROM Category c
The expressions used in the
SELECT
clause have to be single-value. In other words, you
can't have a collection-value path expression in the clause. The path expressions can be an
association field, as in the previous example, where
c.createdBy
is an association field
of the
Category
entity.
The previous query may return duplicate entities. If you want the result to not contain du-
plicate data, use the
DISTINCT
keyword in this way:
SELECT DISTINCT c.categoryName, c.createdBy
FROM Category c
The following
SELECT
statement is invalid
SELECT c.categoryName, c.items
FROM Category
because
c.items
is a collection-type association field, and collection-value path expres-
sions aren't allowed in a
SELECT
clause. We'll talk about using aggregate functions in the
SELECT
clause in the next section.
Using a constructor expression in a SELECT clause
You can use a constructor in a
SELECT
clause to return one or more Java instances. This
is particularly useful when you want to create instances in a query that are initialized with
data retrieved from a subquery:
SELECT NEW actionbazaar.persistence.ItemReport (c.categoryID, c.createdBy)
FROM Category
WHERE categoryId.createdBy = :userName
The specified class doesn't have to be mapped to the database, nor is it required to be an
entity.