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?