Este conteúdo não está disponível no idioma selecionado.

7.7. The where clause


The where clause allows you to narrow the list of instances returned. If no alias exists, you may refer to properties by name:
select cat from Cat cat where cat.name='Fritz'
Copy to Clipboard Toggle word wrap
returns instances of Cat named 'Fritz'.
select foo 
from Foo foo, Bar bar
where foo.startDate = bar.date
Copy to Clipboard Toggle word wrap
will return all instances of Foo for which there exists 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:
select cat 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. If you were to write something like
select foo from Foo foo  
where foo.bar.baz.customer.address.city is not null
Copy to Clipboard Toggle word wrap
you would end up with a query that would require four table joins in SQL.
The = operator may be used to compare not only properties, but also instances:
select cat, rival 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 may be used to reference the unique identifier of an object. (You may also use its mapped identifer property name.). Note that this keyword is specific to HQL.
select cat from Cat as cat where cat.id = 123

select cat from Cat as cat where cat.mate.id = 69
Copy to Clipboard Toggle word wrap
The second query is efficient. No table join is required!
Properties of composite identifiers may also be used. Suppose Person has a composite identifier consisting of country and medicareNumber.
select person from bank.Person person
where person.id.country = 'AU' 
    and person.id.medicareNumber = 123456
Copy to Clipboard Toggle word wrap
select account 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 requires no table join.
Likewise, 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. Once again, this is specific to HQL.
select cat from Cat cat where cat.class = DomesticCat
Copy to Clipboard Toggle word wrap
You may also specify properties of components or composite user types (and of components of components, etc). Never try to use a path-expression that ends in a property of component type (as opposed to a property of a component). For example, if store.owner is an entity with a component address
store.owner.address.city    // okay
store.owner.address         // error!
Copy to Clipboard Toggle word wrap
An "any" type has the special properties id and class, allowing us to express a join in the following way (where AuditLog.item is a property mapped with <any>). Any is specific to Hibernate
from AuditLog log, Payment payment 
where log.item.class = 'Payment' and log.item.id = payment.id
Copy to Clipboard Toggle word wrap
Notice that log.item.class and payment.class would refer to the values of completely different database columns in the above query.
Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat