-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: SQL order by condition
PostPosted: Thu Jan 05, 2006 12:22 am 
Newbie

Joined: Thu Jan 05, 2006 12:17 am
Posts: 2
Hi

We need an SQL like this:

=======
select lpad(code, 5, "0") as paddedcode, * from cat_table order by paddedcode;

OR

select * from cat_table order by lpad(code, 5, "0");

=======

All it needs is an sql alias which is used in the order by field. Hibernate should still populate the object (cat in this example) as normal.

Is this possible using the Criteria API? It looks really simple, but I cannot see how it can be achieved.

Keith


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 12:31 am 
Newbie

Joined: Thu Jan 05, 2006 12:17 am
Posts: 2
I think it can be done using an sqlProjection (with alias). Will give it a go.

Keith


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 04, 2006 7:56 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
did you ever find out the correct syntax for this?


Top
 Profile  
 
 Post subject: sqlProjection
PostPosted: Wed Aug 16, 2006 9:38 pm 
Newbie

Joined: Wed Jul 20, 2005 7:46 pm
Posts: 4
Location: Vancouver Canada
dslevine wrote:
did you ever find out the correct syntax for this?


I didn't manage to find a way to ORDER BY a projection criteria query, nor did I find any postings about it. I gave up and am using HQL.

My code (below) causes the error: org.hibernate.QueryException: could not resolve property: rval of: com.skahasoftware.Ad. This seems to indicate that Order cannot be used with an SQL function.

Criteria crit = session.createCriteria(Ad.class);
crit.setProjection(
Projections.projectionList()
.add(Projections.sqlProjection("{alias}.id theObj",
new String[]{"theObj"},
new org.hibernate.type.Type[]{Hibernate.entity(Ad.class)}))
.add(
Projections.sqlProjection("rand() as rval",
new String[] {"rval"},
new org.hibernate.type.Type[] {new org.hibernate.type.FloatType()}
)
)
);

crit.addOrder(Order.asc("rval"));

_________________
Software Engineering Consultant
www.skahasoftware.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.