• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

'order by' clause in named query

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble attempting to include an 'order by' clause in a Hibernate named query. The named query definition in my hbm.xml looks like:
...
<query name="getMessages">
<![CDATA[
from Message as msg
where msg.conversation = :conversationId
order by :sortField
]]>
</query>
...
The java code that uses the query and sets the conversationId and sortField named parameters looks like:
...
Query query;
Session session;
List messages;

session = getSession(session);

try {
query = session.getNamedQuery( "getMessages" );
query.setInteger( "conversationId", 1 );
query.setString( "sortField", "someString" );
}
messages = query.list();
...

I get the list of messages for the given conversationId. So the 'where' clause and its associated named parameter substitution seems to work ok.

The results are not, however, sorted by the column specified by someString. I've tried using the hibernate property 'name' and 'column' values for the someString value. The results are the same: the list is returned in order by record number. The order by clause has no effect.

Has anyone else out there used an 'order by' clause in a named query? Is this a misuse of a named parameter?
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just a guess:

try order by 'xpto' asc/desc
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by miguel lisboa:
just a guess:

try order by 'xpto' asc/desc



I tried this. It didn't make any difference.

I do have another clue. It seems that the sort works if the property specified in the order by clause has identical property name and column values. If the property has differing name and column values, it doesn't sort, e.g. a data column property defined like so:

<property
column="offset_time"
length="20"
name="offsetTime"
not-null="true"
type="java.lang.Long"
/>

doesn't work. I tried seting the order by criteria to 'offset_time' and 'offsetTime'. The results are the same, and un-ordered.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic