By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,100 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,100 IT Pros & Developers. It's quick & easy.

Hibernate order by query thru java solution urgently reqd

P: 1
i am using java with hibernate . i want to run a hibernate order by query thru java.
i have written the query in hibernate.hbm.xml file. the query is as follows:

SELECT distinct a.process_id, name, a.category_id,c.parent_id
FROM tbl_process_master a, tbl_category c where (lower( like :searchString or lower(a.description) like :searchString or
lower(a.meta_tag) like :searchString ) and a.status=:status and a.category_id=c.cat_id
order by :orderBy

(orderBy is passed from java method as a parameter)
public List getSearchResult(String searchString, status,String orderBy {

the java method calling this query contains follows only the calling of query is written below rest has been omitted as it is not reqd.:

Session ss=s.openSession();
List ProcessList1 = ss.getNamedQuery("getSearchResultForProcess1")
.setString("searchString", searchString)
.setString("status", status)
.setString("orderBy", orderBy)

then i iterate the List.

the problem is the query works fine if i write the script directly from sql command window or i write the query as

above query order by ' " + orderBy+" '

and orderby also works based on the parameter given to it.
but when i try to run the same query thru hibernate ie writing the query in hibernate.hbm.xml file and passing the orderby parameter thru a java method aas described above the query runs but orderby does not happen

The problem is that hibernate converts the orderby parameter as 'orderBy' and then tries to do order by with those there is no col name or col no. with the 'orderBy' the orderby clause does not get fired.
Mar 12 '08 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 1,216
I don't think you can do that. Parameters are for values, not for names of columns. You can not parameterize a query by the column ORDER By column.


You could have multiple queries, one for each column and invoke the appropriates one.

Or you could form the query string in Java using a bit of logic, appending the correct column name.
Mar 12 '08 #2

Expert 10K+
P: 11,448
@BD: *ahem* this thread was marked as 'urgent' *cough*

kind regards,

Jos ;-)
Mar 12 '08 #3

Expert 100+
P: 1,216
@BD: *ahem* this thread was marked as 'urgent' *cough*

kind regards,

Jos ;-)
I tried so send urgent data but OOBINLINE was disabled.
Mar 12 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.