The where clause Use in Hibernate by R4R Team

We use the where clause that allows to refine the list of instances returned. If no alias exists, Now can refer to properties by name

from Student where name='Sobin'

If there is an alias, use a qualified property name:

from Studnet as student where student.name='Sobin'

This returns instances of Student named 'Sobin'.

The following query:

select Zoo from Zoo zoo, Zar zar where foo.startDate = bar.date

It will returns all instances of Zoo with an instance of bar with a date property equal to the startDate property of the Zoo. Compound path expressions make the where clause extremely powerful. This query translates to an SQL query with a table (inner) join. It would result in a query that would require four table joins in SQL.

Consider the following:

from Student student where student.mate.name is not null

For example:

from Zoo zoo where foo.bar.baz.customer.address.city is not null

The = operator can be used to compare not only properties, but also instances:

from Student student, Student rival where student.mate = rival.mate
select student, mate from Student student, Student mate where student.mate = mate

The special property (lowercase) id can be used to reference the unique identifier of an object. "Referring to identifier property” for more information.

from Student as student where student.id = 123

from Student as student where student.mate.id = 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 person.id.country = 'AU' and person.id.medicareNumber = 123456
from bank.Account account where account.owner.id.country = 'AU' and account.owner.id.medicareNumber = 123456

Once again, the second query does not require a table join.  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 Student student where student.class = DomesticStudent

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 ):

from AuditLog log, Payment payment where log.item.class = 'Payment' and log.item.id = payment.id

The log.item.class and payment.class would refer to the values of completely different database columns in the above query.
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!