I'm using Spring Boot and therefore Spring Data to perform my queries. I suppose the best way to create complex queries in Spring Data is with the @Query
annotation. However, I understand that the SELECT
fields in my custom query must match the fields from a given return Class.
How can I create a Spring Data query to match the SQL below?
SELECT latitude, longitude, CS.*,
111.045* DEGREES(ACOS(COS(RADIANS(:lat))
* COS(RADIANS(CS.lat_centroid))
* COS(RADIANS(:long) - RADIANS(CS.long_centroid))
+ SIN(RADIANS(:lat))
* SIN(RADIANS(CS.lat_centroid)))) AS distance_in_km
FROM CensusSector CS
ORDER BY distance_in_km ASC LIMIT 1
PS1: My return object will be CensusSector.
PS2: I don't think it can make any difference but I'm working with MySQL .
Edit 1: As suggested by @Bunti, these are the news I got after trying out some of his points.
in order to use query.setMaxresults
with what you sent I had to follow this post instead of using @Query
with nativeQuery=true
. So I did this and got the following exception:
org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected token: CensusSectorGroup near line 1, column 318