Red Hat Training

A Red Hat training course is available for Red Hat JBoss Web Server

14.10. The Where Clause

The where clause allows you to refine the list of instances returned. If no alias exists, you can refer to properties by name:
from Cat where name='Fritz'
If there is an alias, use a qualified property name:
from Cat as cat where'Fritz'
This returns instances of Cat named 'Fritz'.
The following query:
select foo
from Foo foo, Bar bar
where foo.startDate =
returns all instances of Foo with an instance of bar with a date property equal to the startDate property of the Foo. Compound path expressions make the where clause extremely powerful. Consider the following:
from Cat cat where is not null
This query translates to an SQL query with a table (inner) join. For example:
from Foo foo
where is not null
would result in a query that would require four table joins in SQL.
The = operator can be used to compare not only properties, but also instances:
from Cat cat, Cat rival where cat.mate = rival.mate
select cat, mate
from Cat cat, Cat mate
where cat.mate = mate
The special property (lowercase) id can be used to reference the unique identifier of an object.
from Cat as cat where = 123

from Cat as cat where = 69
The second query is efficient and does not require a table join.
Properties of composite identifiers can also be used. Consider the following example where Person has composite identifiers consisting of country and medicareNumber:
from bank.Person person
where = 'AU'
    and = 123456
from bank.Account account
where = 'AU'
    and = 123456
Once again, the second query does not require a table join.
Refer to Section 14.6, “Referring to Identifier Property” section for more information regarding referencing identifier properties.
The special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A Java class name embedded in the where clause will be translated to its discriminator value.
from Cat cat where cat.class = DomesticCat
You can also use components or composite user types, or properties of said component types.
An "any" type has the special properties id and class that allows you to express a join in the following way (where AuditLog.item is a property mapped with <any>):
from AuditLog log, Payment payment
where log.item.class = 'Payment' and =
The log.item.class and payment.class would refer to the values of completely different database columns in the above query.