9.17.3 SELECT clause
The SELECT clause expresses the query results. One or more values are returned from the SELECT clause of the query. You specify the following elements, demarcated by commas, in the SELECT clause. Note that the cluster demarcated by commas is called a select expression.
-
Identifier of the abstract schema or persistence field that is assigned an identifier
-
Path expression
-
Set function
-
Constructor expression
Specify the DISTINCT keyword when you want to exclude the duplicated values from the query result.
An example of coding the SELECT clause is as follows:
SELECT e.employeeName, e.monthlySalary FROM Employee AS e WHERE e.monthlySalary < 150000
- Organization of this subsection
(1) Constructor expression
The constructor expression is used in the select expression of the SELECT clause that returns one or more Java instances. The generated name specifies the fully qualified name.
The constructor expression can be obtained as a combination of some or all entity columns and the other related entity columns. Note that the class that stores this result need not be an entity.
Specify the syntax of the constructor expression by assigning the NEW operator in the select expression. If the class that stores the result is an entity, the state of the entity class instance becomes new. An example of coding the constructor expression is as follows:
SELECT NEW com.hitachi.jpa.test.entity.EmployeeTmp (e.employeeId, e.employeeName, d.departmentName) FROM Department AS d, d.employees AS e WHERE e.employeeId = 12003
(2) Set function
You can use the set function with the SELECT clause. The following table lists and describes the available set functions.
Set function |
Argument |
Result type |
Result when the applied value does not exist |
---|---|---|---|
AVG |
Numeric type state field # |
Double type |
null |
MAX |
Field type that can specify the order (numeric type, string type, character type, or date type) # |
Type of the applied field |
null |
MIN |
Field type that can specify the order (numeric type, string type, character type, or date type) # |
Type of the applied field |
null |
SUM |
Numeric type state field # |
|
null |
COUNT |
Identification variable (when you specify the path expression in the argument, specify the state field or relation field) |
Long type |
0 |
For details on the syntax of the set function expression, see Appendix G BNF for JPQL.
(3) Execution results of the SELECT clause
The type of the query results defined in the SELECT clause of the query is one of the following. If multiple types are present, the types are serialized.
-
Entity abstract schema type
-
Field type
-
Set function results
-
Constructor expression results
The result type of the SELECT clause is defined according to the result type of the select expression included in the SELECT clause. If multiple select expressions are used in the SELECT clause, the query result is Object[] type. The elements of this result match the order specified in the SELECT clause and with the result type of each select expression.