Ce contenu n'est pas disponible dans la langue sélectionnée.

14.9. 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'
Copy to Clipboard Toggle word wrap
If there is an alias, use a qualified property name:
from Cat as cat where cat.name='Fritz'
Copy to Clipboard Toggle word wrap
This returns instances of Cat named 'Fritz'.
The following query:
select foo
from Foo foo, Bar bar
where foo.startDate = bar.date
Copy to Clipboard Toggle word wrap
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 cat.mate.name is not null
Copy to Clipboard Toggle word wrap
This query translates to an SQL query with a table (inner) join. For example:
from Foo foo
where foo.bar.baz.customer.address.city is not null
Copy to Clipboard Toggle word wrap
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
Copy to Clipboard Toggle word wrap
select cat, mate
from Cat cat, Cat mate
where cat.mate = mate
Copy to Clipboard Toggle word wrap
The special property (lowercase) id can be used to reference the unique identifier of an object. See Section 14.5, “Referring to identifier property” for more information.
from Cat as cat where cat.id = 123

from Cat as cat where cat.mate.id = 69
Copy to Clipboard Toggle word wrap
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 person.id.country = 'AU'
    and person.id.medicareNumber = 123456
Copy to Clipboard Toggle word wrap
from bank.Account account
where account.owner.id.country = 'AU'
    and account.owner.id.medicareNumber = 123456
Copy to Clipboard Toggle word wrap
Once again, the second query does not require a table join.
See Section 14.5, “Referring to identifier property” 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
Copy to Clipboard Toggle word wrap
You can also use components or composite user types, or properties of said component types. See Section 14.17, “Components” for more information.
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 log.item.id = payment.id
Copy to Clipboard Toggle word wrap
The log.item.class and payment.class would refer to the values of completely different database columns in the above query.
Retour au début
Red Hat logoGithubredditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance. Découvrez nos récentes mises à jour.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez le Blog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

Theme

© 2025 Red Hat