Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order by clause

 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I've got the following HQL:



Basically I want to convert the periodFrom to a String and put that into a list, but it should be sorted by the Date value that PeriodFrom is.

Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type is period? If its a data can you not just order by it without the function?
 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:What type is period? If its a data can you not just order by it without the function?


Period is an alias for a converted periodFrom, so in other words a string.



 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so you can't sort this chronologically since it is text. Another question: why do you convert to character data in the first place? Could you not just treat tge periodFrom property as a java.sql.Date?
 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:Ok, so you can't sort this chronologically since it is text. Another question: why do you convert to character data in the first place? Could you not just treat tge periodFrom property as a java.sql.Date?


Exactly.

I could probably treat the periodFrom as a date, and use a List<Date> instead of a List<String>. Probably would work.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its a general rule of thumb (well, one of mine anyway ) that if formatting logic appears in a SQL or HQL query something has gone wrong with the design somewhere. If you need to format stuff, do it after the query has returned results, assuming you don;t need the formatting to get the results in the first place, which with Dates you should not.
 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:Its a general rule of thumb (well, one of mine anyway ) that if formatting logic appears in a SQL or HQL query something has gone wrong with the design somewhere. If you need to format stuff, do it after the query has returned results, assuming you don;t need the formatting to get the results in the first place, which with Dates you should not.


hmm... good point.... i am coming from a plsql background so its a bit hard NOT to do as much formatting etc as possible in the sql
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic