Hibernate aliases

My colleague Raj was recently updating a Hibernate criteria query which made use of aliases. He wanted to add a criterion that a value in a linked table must be null, so the most obvious change to make was to add another alias and the appropriate condition. e.g.

.addAlias("product","p")
.add(Restrictions.isNull(p.sku)

However, this query produced no results. Why? The answer lies in how an alias works. Creating an alias actually performs an inner join. However, on this occasion, the “sku” column is the foreign key that is used for the join. Hence, the join produces no results. To achieve the desired query, you can use the overloaded version of the addAlias method, that allows to chose the join type:

createCriteria(String associationPath, int joinType)

Alternatively, you can remove the alias and simply tweak the syntax of the restriction.

This entry was posted in Hibernate and tagged . Bookmark the permalink.

One Response to Hibernate aliases

  1. Chandra sekhar melam says:

    Sir, Your articles are good… Im new to hibernets but your articles are gives little bit confident to move…. so if you dont mind, can you please forward to hibernet notes to my mail: melam.chandra.sekhar@gmail.com… Thank you in advance

Leave a Reply

Your email address will not be published. Required fields are marked *

503,487 Spambots Blocked by Simple Comments

HTML tags are not allowed.