Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate criteria: how to order by two columns concatenated?

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?

like image 691
Georgie Porgie Avatar asked Jul 19 '11 20:07

Georgie Porgie


People also ask

Can we use order by in HQL?

Order by orders the data on the basis of given property in ascending or descending order. Group by groups the data on the basis of given property. In HQL we perform order by and group by for the given property of entity or associated entities.

What is criteria uniqueResult ()?

uniqueResult() Convenience method to return a single instance that matches the query, or null if the query returns no results.

Can you explain criteria in hibernate?

Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API, which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.


1 Answers

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.

like image 146
toto2 Avatar answered Sep 18 '22 13:09

toto2