Is it possible to remove order from Hibernate Criteria?
Asked Answered
H

2

9

If I have an @OrderBy("someProperty") annotation on an object and then use a Criteria to add an ORDER BY clause like so:

criteria.addOrder(Order.asc("id"));

The resulting SQL will do the ordering like this:

ORDER BY someProperty, id asc

Is it possible to change the order of the two or to remove the someProperty order? I can't remove the @OrderBy annotation and I'm using Hibernate for Java.

Haletky answered 21/9, 2010 at 9:58 Comment(2)
I have this same question... we are using Hibernate 3.2.6... would love the ability to 'override' the @OrderBy annotation when using projections. Does anyone know about this? Is there a way to change it on the fly? Change either the order of the orders, or remove the @OrderBy clause all together? Thanks!Chiropractic
I'm having the same problem with Hibernate 3.6.4, which I've narrowed down to the (bugged) implementation of org.hibernate.loader.JoinWalker#orderBy(final List associations, final String orderBy) on line 820. I believe there is a bug at this line, since the OrderBy on the associations is being merged before the ordering specified on the Criteria. Think about it: if I've specified a desired ordering in a Criteria having Items as the root entity, should Hibernate really be placing the OrderBy specified on the associated Bids first?Buckle
T
2

Criteria has no methods for removal of Order neither Criterion Order class is very limited, you can only use property names and it generates standard and portable SQL. OrderBy annotation is a SQL order, as javadoc states: OrderBy That means you can use there any sql (even a exclusive one of your database vendor). Take a look at these article: Sorting Collections in Hibernate Using SQL in @OrderBy

Adding a SQL fragment into your domain class isn't necessarily the most elegant thing in the world, but it might be the most pragmatic thing.

Talky answered 12/7, 2011 at 2:25 Comment(0)
G
2

It may be possible to remove particular ordering via criteria.iterateOrderings() iterator, but I'm not sure how it works with annotations.

Gastritis answered 10/3, 2013 at 21:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.