Java Reference
In-Depth Information
While using path expressions, keep in mind that you can't navigate through the collection-
value path expressions to access a persistence or association field as in the following ex-
ample:
c.items.itemName or c.items.seller
This is due to the fact you can't access an element of a collection, and
items
is a
collection of items. Using
c.items.itemName
in JPQL is similar to using
cat-
egory.getItems().getItemName()
, and this isn't allowed. Next, you'll see how
you can use path expressions in a
WHERE
clause.
Filtering with WHERE
The
WHERE
clause in JPQL allows you to filter the results of a query. Only entities that
match the query condition specified will be retrieved. Say you want to retrieve all instances
of the
Category
entity; you can use a JPQL statement without a
WHERE
clause:
SELECT c
FROM Category c
Using this code will probably result in thousands of
Category
instances. But say you ac-
tually want to retrieve instances of a
Category
by a specific condition. To retrieve the
Category
instances that have a
categoryId
greater than 500, you'd have to rewrite
the query like this:
SELECT c
FROM Category c
WHERE c.categoryId > 500
Almost all types of Java literals such as
boolean
,
float
,
enum
,
String
,
int
, and so
forth are supported in the
WHERE
clause. You can't use numeric types such as octal and
hexadecimals, nor can you use array types such as
byte[]
or
char[]
in the
WHERE
clause. Remember that JPQL statements are translated into SQL; SQL is actually imposing
the restriction that BLOB and CLOB types can't be used in a
WHERE
clause.