Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate order by association

I'm using Hibernate 3.2, and using criteria to build a query. I'd like to add and "order by" for a many-to-one association, but I don't see how that can be done. The Hibernate query would end up looking like this, I guess:

select t1.a, t1.b, t1.c, t2.dd, t2.ee
from t1
inner join t2 on t1.a = t2.aa
order by t2.dd   <-- need to add this

I've tried criteria.addOrder("assnName.propertyName") but it doesn't work. I know it can be done for normal properties. Am I missing something?

like image 530
Gary Kephart Avatar asked May 15 '09 17:05

Gary Kephart


People also ask

How to sort in Hibernate Criteria?

Setting the Sorting Order. The Order class has two methods to set the sorting order: asc(String attribute) : Sorts the query by attribute in ascending order. desc(String attribute) : Sorts the query by attribute in descending order.

Which is sorted in normal order in hibernate?

In hibernate a sorted collection is sorted in memory being Java the responsible of sorting data using compareTo method. Obviously this method is not the best performance-way to sort a collection of elements.

What are true about ordering collections in hibernate?

- The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator. - The efficiency depends on the size of the collection. - Ordered collection is sorted by specifying the order-by clause for sorting this collection when retrieval.

What is hibernate criteria?

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.


2 Answers

Ok, found the answer. I tried something that I didn't think would work, but to my surprise did. I was trying this:

Criteria criteria = super.getSession().createCriteria(WipDiscreteJob.class);

criteria.addOrder(Order.asc("assnName.propertyName"))

but what actually worked was:

Criteria criteria = super.getSession().createCriteria(WipDiscreteJob.class);
Criteria assnCrit = criteria.createCriteria("assnName");

assnCrit.addOrder(Order.asc("propertyName"));

I had made the assumption that the addOrder() method was only usable on the main criteria and not on any association criteria.

like image 129
Gary Kephart Avatar answered Oct 04 '22 04:10

Gary Kephart


I was having the same issue and it can also be solved like this:

Criteria criteria = super.getSession().createCriteria(WipDiscreteJob.class)
  .createAlias("assnName","a")
  .addOrder(Order.asc("a.propertyName"));

createAlias lets you keep your criteria rooted on your original entity (WipDiscreteJob.class in this case) so that you can keep building your criteria up in case you needed it (for example, if you need a second order by property from you original entity).

like image 35
Marcelo Avatar answered Oct 04 '22 03:10

Marcelo