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.  [ 1 post ] 
Author Message
 Post subject: Criteria - Novice question
PostPosted: Fri Nov 12, 2010 12:23 pm 
Newbie

Joined: Tue Nov 09, 2010 12:45 pm
Posts: 7
Hi,
I am trying to do something that I think should be fairly simple but so far haven't had any luck.

what I would like to do is to add some sql to the select part of a Criteria object, I want to add SELECT myFunction AS distance so that I can later order by distance.

The function I want to insert is :-
Code:
(
SQRT( POW( 69.1 * ( addr.latitude - 51.3814282 ) , 2 ) + POW( 69.1 * ( - 2.3574537 - addr.longitude ) * COS( addr.latitude / 57.3 ) , 2 ) )
) AS distance


My current criteria looks like (I already use the equation to limit the results by distance - I want to be able to sort by distance as well though...):-

Code:
Criteria addr = criteria.createCriteria("location.address");
                addr.add((Restrictions.and(Restrictions.between("latitude", latmin,     
                       latmax), Restrictions.between("longitude", truelongmin, truelongmax))));

                String sql =  "SQRT( POW( 69.1 * ( {alias}.latitude - " + point[1]     
                       +" ) , 2 ) + POW( 69.1 * ( "+point[0] +" - {alias}.longitude ) * COS( {alias}.latitude /"
                       +" 57.3 ) , 2 ) )  < "+distance;     
                addr.add(Restrictions.sqlRestriction(sql));


Any suggestions would be appreciated.

Cheers,
Rob


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

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.