Hibernate criteria: how to order by two columns concatenated?

Georgie Porgie picture Georgie Porgie · Jul 19, 2011 · Viewed 43.2k times · Source

I have a Person table which has two columns: first_name and last_name. The Person class has two corresponding fields: firstName and lastName. Now I'm using criteria api and trying to create an order by based on these two columns concatenated. Is it possible? Or it can only be achieved by hql?

Answer

toto2 picture toto2 · Jul 19, 2011

Here an example for the JBoss hibernate site:

from DomesticCat cat order by cat.name asc, cat.weight desc, cat.birthdate

Or from the same website, for the Criteria api:

List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "F%")
.addOrder( Order.asc("name") )
.addOrder( Order.desc("age") )
.setMaxResults(50)
.list();

They seem to be quite fond of cats at JBoss.