-->
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: Database query with calculation
PostPosted: Thu Jan 13, 2011 12:22 pm 
Newbie

Joined: Thu Jan 13, 2011 12:01 pm
Posts: 2
Hi, hoping somebody can point me in the right direction with a problem I'm having.

I need to retrieve a list of locations based on their vicinity to a given Place. So for example, I have "Place" and "Attraction" as mapped entities, both of which have a lat/lng.

So in SQL I could do something like:

Code:
select a.*, SQRT(POWER((69.1 * (a.latitude - PLACE_LAT)) , 2 ) + POWER((53 * (a.longitude - PLACE_LNG)), 2)) as distance from Attraction a where SQRT(POWER((69.1 * (a.latitude - PLACE_LAT)) , 2 ) + POWER((53 * (a.longitude - PLACE_LNG)), 2)) < 20;


which would get me all the Attractions with 20m of the Place, assuming I provide PLACE_LAT and PLACE_LNG to the query from my Place object.

I couldn't find a nice way to do this with Criteria, so I have now moved onto HQL and got a query which I believe works.

Code:
select a, sqrt((69.1 * (a.latitude - :lat) * 69.1 * (a.latitude - :lat)) + (53 * (a.longitude - :lng) * 53 * (a.longitude - :lng))) as distance from Attraction a where sqrt((69.1 * (a.latitude - :lat) * 69.1 * (a.latitude - :lat)) + (53 * (a.longitude - :lng) * 53 * (a.longitude - :lng))) < 20


However, my extra column "distance" now means that the objects returned from query.list() aren't of type "Attraction", they're just "Object".

I have tried declaring distance in Attraction but this creates a column in the database which I do not want, and still doesn't work. I tried declaring distance as Transient but this still doesn't work.

Any ideas? Do I need a whole new object for this result set?


Top
 Profile  
 
 Post subject: Re: Database query with calculation
PostPosted: Thu Jan 20, 2011 7:25 am 
Newbie

Joined: Thu Jan 13, 2011 12:01 pm
Posts: 2
I have discovered that this query returns a List of Arrays as follows, which are pairs of Attraction objects and my new distance attribute.

Therefore you can do this:

Code:
List<Object[]> results = q.list();
      
for(Object[] result : results)
{
   Attraction a = (Attraction)result[0];
   double d = (Double)result[1];
   a.setDistance(d);
}


Feel free to close this topic.


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.