-->
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.  [ 2 posts ] 
Author Message
 Post subject: Restrictions / Criteria Beginner Question
PostPosted: Tue Nov 09, 2010 12:54 pm 
Newbie

Joined: Tue Nov 09, 2010 12:45 pm
Posts: 7
Hi I have been trying to work out the best way to do this today to no avail.

What I would ideally like to do is to create an alias distance calculated by the SQL formula below (although I am open to other ways of calculating the distance, this was just the way that seemed it should be easiest)

Once I have that alias I want to be able to use it in a Restrictions fashion to find all that are within a certain distance.

I would also like to be able to sort via distance.

This is part of a bigger search criteria that is built up so I would ideally like to keep using Criteria. ( I already limit the range of Lat and Long values to make the distance calculation required on less fields.

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


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


Currently I have addr3_ in the sql query because I was looking at the verbose output and that is the way that Hibernate has generated the query (this hack worked in the one instance i was looking at but I dread to think about the longer term implications so would not want it to stay there!!)


Top
 Profile  
 
 Post subject: Re: Restrictions / Criteria Beginner Question
PostPosted: Thu Nov 18, 2010 12:02 pm 
Newbie

Joined: Tue Nov 09, 2010 12:45 pm
Posts: 7
The trick was to create a root at the location.address Criteria, and then use the {alias} function.
eg.

Code:
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;     

Criteria criteria = session.createCriteria(Activity.class)
                .createAlias("activityLocations", "actloc")
                .createAlias("actloc.location", "location");
            Criteria locaddr = criteria.createCriteria("location.address");
            locaddr.add((Restrictions.and(Restrictions.between("latitude", latmin,     
                   latmax), Restrictions.between("longitude", truelongmin, truelongmax))));    
            locaddr.add(Restrictions.sqlRestriction(sql));


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.