C.4. WHERE ClauseThe WHERE clause of an EJB QL query is used to specify conditional logic that is used to select a subset of the beans references in the FROM clause. Any beans that you want to refer to in your conditional query logic must be declared in the FROM clause of the query. The syntax of the WHERE clause mirrors the syntax of SQL WHERE clauses. The WHERE clause is made up of literals and variables composed into various conditional expressions. The variables used in a WHERE clause can be query variables declared in the FROM clause, path expressions derived from the query variables, or input parameters provided in the arguments to the corresponding finder or select method. The conditional expressions in the WHERE clause can be composed using arithmetic operators, comparison operators, or logical operators. C.4.1. LiteralsLiterals are strings, numeric values, or Boolean values. String literals are enclosed in single quotes: SELECT OBJECT(p) FROM ProfileBean p WHERE p.name = 'Fred' If you want to include a single quote in a literal, use two single quotes: SELECT OBJECT(p) FROM ProfileBean p WHERE p.name = 'Fred''s profile' Numeric literals can be either exact integer values or approximate floating-point values. Integer values are specified using a sequence of digits with no decimal point: SELECT OBJECT(prof) FROM ProfileBean prof WHERE prof.id < 1000 Floating-point values are specified using either scientific notation or a numeric value with a decimal point: SELECT OBJECT(p) FROM Person p WHERE p.salary > +5e4 AND p.overhead < 1.32 Floating-point values can be in the range supported by the Java double type. Boolean literals are specified using true or false. These values are case-insensitive. C.4.2. VariablesVariables used in expressions in a WHERE clause can be query variables declared in the FROM clause, input parameter variables that refer to arguments to the finder or select method, or path expressions based on query variables or input parameter variables. C.4.2.1. Simple query variablesQuery variables can be used directly in conditional expressions when it is necessary to directly compare abstract schema types. This makes sense only in comparison expressions in which the equality of two entity beans is being compared. For example: SELECT OBJECT(p) FROM Person p1, Person p2, Organization o1, Organization o2 WHERE o1 = p1.employer AND o2 = p2.employer AND o1 = o2 AND ... In this case we're comparing the two Organization beans for equality. Equality between entity beans is based on the equality of their primary keys, (e.g., if the two primary keys are equal, then the two entities are considered equal). C.4.2.2. Input parameter variablesInput variables refer to the arguments of the query's finder or select method. They are specified using a $ followed by the integer index (starting with 1) of the method argument. For example, if we are defining a query for a finder method declared with the following signature: abstract public Collection findEmployees(String companyName); We can refer to the string argument in the query using an input parameter variable: SELECT OBJECT(e) FROM Person e, Organization o WHERE o.name = $1 AND e.employer = o If the input variable is an entity bean object with an abstract schema type, you can also use the input parameter variable in path expressions. For example, if we have a finder method on our Person bean: abstract public Collection findCoWorkers(Person p); a corresponding query might be: SELECT OBJECT(w) FROM Person w, Organization o WHERE $1.employer = o AND w.employer = o Input parameter variables can be used only in comparison expressions that use the basic comparison operators and the MEMBER OF comparison operator, as described in "Comparison operators" later in this appendix. C.4.2.3. Path expression variablesPath expressions are specified using either query variables or input parameter variables. A path expression is a variable followed by one or more dot-delimited CMP field or CMR field references. In one of our earlier examples, we used a path expression to refer to the name CMP field on the Organization bean: SELECT OBJECT(e) FROM Person e, Organization o WHERE o.name = $1 AND e.employer = o Here, the path expression o.name uses the query variable Organization o declared in the FROM clause and then refers to the CMP field name on the variable. References to cmr-fields on variables can be referenced further in multilevel path expressions. For example, a simplified version of the previous query is: SELECT OBJECT(e) FROM Person e WHERE p.employer.name = $1 Here, we're referencing the CMR field employer on the Person bean, then further referencing the CMP field name on the resulting Organization. Referencing CMR fields in path expressions is possible only if the relationship is navigable from the bean at the root of the reference. So in this case, we're assuming that the employer CMR field is navigable from the Person bean, which allows us to reference it as an Organization and then further reference the name CMP field on the Organization. Path expressions can evaluate to either single values or collection values. If, for example, a CMR field is one-to-many or many-to-many, a reference to it in a path expression is collection-valued. Path expressions that are collection-valued can be used only in comparison expressions using the IS EMPTY operator or the MEMBER OF operator (described in "Comparison operators" later in this appendix). C.4.3. FunctionsEJB QL also includes several built-in functions that can be applied to literals, variables, or values derived from other expressions. They are listed in Table C-1.
C.4.4. Conditional ExpressionsConditional expressions are composed of a sequence of one or more other expressions, joined together by logical AND or OR operators. A single expression is made up of literals and/or variables joined by operators to result in a Boolean value. Conditional expressions follow the usual evaluation/precedence rules: they are evaluated left to right, taking into account operator precedence rules (as discussed next) and grouping of expressions using parentheses. The operators available in EJB QL for creating conditional expressions fall into three categories: logical operators , arithmetic operators , and comparison operators . C.4.4.1. Logical operatorsThe logical operators in EJB QL are NOT, AND, and OR, in precedence order, with the usual semantics: NOT is the unary negation operator, AND is the binary intersection operator, and OR is the binary union operator. These can be applied to any expressions that evaluate to a Boolean value. C.4.4.2. Arithmetic operatorsArithmetic operators are applied to numeric variables and literals. They are listed in Table C-2.
C.4.4.3. Comparison operatorsThe comparison operators are used to perform various value and range comparisons on variables and are listed in Table C-3. Some comparison operators can be applied only to variables of a certain typethese restrictions are mentioned in the description where applicable.
|