I have the following query:
public List<Object> findNearbyGangs(double lat, double lng, double distance) {
Query query = this.entityManager.createNativeQuery("SELECT id, (6371 * acos (cos(radians(:latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians(:longitude)) + sin(radians(:latitude)) * sin(radians(latitude)))) AS distance FROM Gang g GROUP BY id HAVING distance < :distance ORDER BY distance")
.setParameter("latitude", lat)
.setParameter("longitude", lng)
.setParameter("distance", distance);
List<Object> objects = query.getResultList();
return objects;
}
which objects contain:
[
[
3,
0
],
[
321,
0
],
[
353,
1.3575295456440253
]
]
How do I parse this object so that id = 3 and distance = 0, id = 321 and distance = 0, and so on?
I asked on Stackoverflow but got no response so I ended up figuring it out myself:
public List<NearbyGang> getNearbyGangs(Double longitude, Double latitude, int radius) {
List<Object> nearbyGangsFromDatabase = this.gangRepositoryImpl.findNearbyGangs(longitude, latitude, radius);
List<NearbyGang> nearbyGangs = new ArrayList<NearbyGang>();
for (Object object : nearbyGangsFromDatabase) {
Class oClass = object.getClass();
if (oClass.isArray()) {
for (int i = 0; i < Array.getLength(object); i++) {
BigInteger gangId = (BigInteger) Array.get(object, 0);
Gang gang = this.getById(gangId.longValue()).get();
Double distance = (Double) Array.get(object, 1);
NearbyGang nearbyGang = new NearbyGang(gang, distance);
nearbyGangs.add(nearbyGang);
}
}
}
return nearbyGangs;
}
Improvements welcomed.