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.
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 |
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 |
You can use the set function with the SELECT clause. The following table lists and describes the available set functions.
Table 6-19 Set functions available in the SELECT clause of JPQL
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 |
Note: Regardless of whether DISTINCT is specified, the null value is removed before the set function is applied.
# If the path expression is specified in the argument, you cannot specify the relation field.
For details on the syntax of the set function expression, see Appendix D BNF for JPQL.
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.
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.