-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Ordering by an expression with Criteria API?
PostPosted: Tue Nov 03, 2009 1:13 am 
Newbie

Joined: Sat Jan 06, 2007 3:46 pm
Posts: 15
Hi. I have a table of events with a date column. I'd like to fetch all the rows in the table, and order them like this (in MySQL):

Code:
order by event_date < now(), event_date


So that the next event to occur is at the top, followed by the one after that, etc, and then followed after that by past events.

I don't think the Order API lets me specify an expression to order by, as it only accepts property names. I'm wondering if there's a way to do this that isn't obvious in the docs. Thanks!


Top
 Profile  
 
 Post subject: Re: Ordering by an expression with Criteria API?
PostPosted: Tue Nov 03, 2009 9:53 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
you need to subclass Order:
Code:
public class SQLOrder extends Order {

    private static final long serialVersionUID = 1L;
    private final static String PROPERTY_NAME = "uselessAnyways";
    private boolean ascending;
    private String sql;

    protected SQLOrder(String sql, boolean ascending) {
        super(PROPERTY_NAME, ascending);
        this.sql = sql;
        this.ascending = ascending;

    }

    @Override
    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
            throws HibernateException {
        StringBuilder fragment = new StringBuilder();
        fragment.append("(");
        fragment.append(sql);
        fragment.append(")");
        fragment.append(ascending ? " asc" : " desc");
        return StringHelper.replace(fragment.toString(), "{alias}",
                criteriaQuery.getSQLAlias(criteria));
    }

}


now you can use it when assembling your criteria.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Ordering by an expression with Criteria API?
PostPosted: Thu Jan 28, 2010 5:50 pm 
Beginner
Beginner

Joined: Wed Jul 30, 2008 8:43 am
Posts: 32
Hi.

can the same be done for HSQL? I need two things:

1. possibility to order by alias:

Code:
select ord, (select sum(pays.income) from ord.payments pays) as sumIncome from Order as ord order by sumIncome


2. possibility to add NULLS FIRST/NULLS LAST


Top
 Profile  
 
 Post subject: Re: Ordering by an expression with Criteria API?
PostPosted: Thu Jan 28, 2010 5:53 pm 
Newbie

Joined: Sat Jan 06, 2007 3:46 pm
Posts: 15
mmerder wrote:
you need to subclass Order:
[/code]

now you can use it when assembling your criteria.


Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.