Hi,
i'm trying to achieve the same thing, i.e using a named parameter to dynamically create an order by clause. However i'm not using a named query but a standard HQL query built in the code.
Code:
EntityManager em=getEntityManager();
StringBuffer queryString=new StringBuffer("select s from mytable s where s.user=:user order by :order asc");
query=em.createQuery(queryString.toString());
query.setFirstResult(first);
query.setMaxResults(pageSz);
query.setParameter("user", getUser());
query.setParameter("order", sortColumnName);
List l=query.getResultList();
The result does not take the ordering constraint into account.
I've turned on logging on the MySQL server and i see that the query is being run as the following:
Code:
select s0_.Id as Id4_, s0_.user as user4, s0_.mobile as mobile2_4_, s0_.displayname as displayn3_4_ from p.mytable s0_ where s0_.user=1 order by 'Displayname' asc limit 3;
I see that the sort column name is being included in quotes, which makes the order by ineffective. Quotes should not be added to the column name.
Has someone seen this behaviour ? I cannot find a way to get those quotes removed :(
I'm running Hibernate 3.2 with Hibernate Annotations and Hibernate EntityManager 3.2.0 CR2. My database is MYSQL 5.0.22.
Any help would be appreciated !
Thanks in advance.