I am trying to debug why my dynamic specification of the order by column is not working. I set showsql to true and below is what I get (i.e. question marks where there should be the value of my variable). It works just fine when I hard code the name of the column I want to order by, but when I try to do a variable substitution it doesn't seem to work. I am guessing when I use setParameter, setString, or setText on the orderByColumn variable that it is putting quotes around the value in the order by clause. I am wondering how others are doing this? I know I can get around it by developing the query string outside of Hibernate first and then feed it to the .createQuery method, but them I lose Hibernates nice features.
Thanks in advance for any help,
Aaron Bartell
Hibernate version:
Hibernate 2.1.3
Name and version of the database you are using:
MySQL 4.1
The generated SQL (show_sql=true):
Hibernate: select document1_.uid as uid, document1_.name as name, document1_.descr as descr, document1_.created as created, document1_.createdby as createdby, document1_.guid as guid, document1_.uploadbytes as uploadby7_, document1_.uploadname as uploadname, document1_.uploadtype as uploadtype, document1_.status as status, document1_.perdocpricecode as perdocp11_, document1_.perpagepricecode as perpage12_, document1_.activedate as activedate, document1_.deactivedate as deactiv14_, document1_.pages as pages from userdocumentasc userdocume0_, document document1_ where (userdocume0_.documentuid=document1_.uid )and(userdocume0_.useruid=? ) order by ? DESC
Java Code
Code:
Query qry = null;
qry = DBConn
.sess()
.createQuery(
"select doc from com.dao.Userdocumentasc uda,"
+ " com.dao.Document doc"
+ " where uda.documentuid=doc.uid and uda.useruid=:useruid order by :orderByColumn DESC");
qry.setInteger("useruid", useruid.intValue());
qry.setString("orderByColumn", orderByColumn);