WHERE Clause

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

Literals

Literals are either 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 ...

Get Java Enterprise in a Nutshell, Second 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.