woyaru
woyaru

Reputation: 5624

How to use clause in order in Hibernate

I would like to get objects via Hibernate from database with concrete order. This order is something like that:

as the first I would like to get objects with column titled for example first_column not null,

as the second I would like to get objects with column second_column not null,

as the last I would like to get objects which third_column is the id for another object/table, and this another object has a field with concrete value for example: "something".

I have created criteria in this way:

criteria.addOrder(Order.asc("firstColumn"));
criteria.addOrder(Order.asc("secondColumn"));

but how can I meet the last requirement? With the restriction I can do something like that:

criteria.createAlias("thirdColumn", "t");
criteria.add(Restrictions.eq("t.field", "something"));

But I have to use order, not restriction with three separate Criteria results, because I am using also setFirstResult() and setMaxResults() of the Criteria to implement pagination in my frontend.

Upvotes: 0

Views: 179

Answers (2)

Vincent Lochen
Vincent Lochen

Reputation: 141

I think you can simply use the "." separator and write your code as follow

criteria.createAlias("thirdColumn", "t");
criteria.addOrder(Order.asc("t.field"));

Upvotes: 1

Pace
Pace

Reputation: 43817

If you can write the statement in SQL then you can probably get away with the approach mentioned in this post which is to create a custom subclass of Order.

Upvotes: 1

Related Questions