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.
1 Response to Hibernate aliases