-->
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.  [ 8 posts ] 
Author Message
 Post subject: Ordering Results in createCriteria
PostPosted: Tue Jan 15, 2008 8:47 am 
Newbie

Joined: Mon Dec 10, 2007 8:18 am
Posts: 5
I've been looking for days on how to order my results. I think I have narrowed it down that I have to do this on the session level.

Here is my line of code:

Code:
HibernateUtils.getSession().createCriteria(EventLocator.class).addOrder( Order.asc("event_date") ).list();

It doesn't seem to be ordering by the event_date as I would like.

The previous code was just:

Code:
HibernateUtils.getSession().createCriteria(EventLocator.class).list();

I had to import the org.hibernate.criterion.Order class to get it to compile. But my results don't come back ordered at all. Any info would help!
Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 10:04 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Is event_date a real date type, or it's String representation?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 10:59 am 
Newbie

Joined: Mon Dec 10, 2007 8:18 am
Posts: 5
gonzao_diaz wrote:
Is event_date a real date type, or it's String representation?


event_date is the actual name of the column in the database.

eventDate is it's String representation.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 11:55 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Do you have a property returning the exact event_date, as a date?
A criteria query has to order by that, not by your string representation.
Unless you have already a getter called "getEvent_date" (I doubt it), the sorting is meaningless.
In general, HQL deals with properties, not fields.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 2:46 pm 
Newbie

Joined: Mon Dec 10, 2007 8:18 am
Posts: 5
gonzao_diaz wrote:
Do you have a property returning the exact event_date, as a date?
A criteria query has to order by that, not by your string representation.
Unless you have already a getter called "getEvent_date" (I doubt it), the sorting is meaningless.
In general, HQL deals with properties, not fields.


In my mapping file, I have:
Code:
<property name="eventDate" column="event_date" type="java.lang.String" not-null="true"/>

In my class file, I have:
Code:
private String m_eventDate = "";

public String getEventDate() {
   return m_eventDate;
}
public void setEventDate(String p_date) {
   m_eventDate = p_date;
}

So I think I got it working with this:
Code:
HibernateUtils.getSession().createCriteria(EventLocator.class).addOrder( Order.asc("eventDate") ).list();

Thank you so much for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 4:24 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
no, you didn't.
If your column is date, it should be mapping to date.
If you want to have a nicely formatted date for your user interface, you can create a getter that returns a String, maybe using a SimpleDateFormat r something alike, but the ordering must be done through the real date, mapped as a date.

If the ordering seems to work now, is just because whatever transformation is being done from date to string (maybe in your local date format) produces a sorting "by string representation" that is similar to the sorting by real date.
But sorting by string representation is useless and dangerous, you should sort by date.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 8:01 am 
Newbie

Joined: Mon Dec 10, 2007 8:18 am
Posts: 5
gonzao_diaz wrote:
no, you didn't...

I don't really have the column in the database declared as date, its just a varchar (It is like this for a few reasons). I have the struts validation making it a strict date format, so sorting by string still works. Ex: 01-01-2008 is before 01-02-2008.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 12:57 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Based on your String value 03-01-2007 would sort before 04-01-2006.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.