I am having below query.
SELECT zip, primary_city, latitude, longitude,
111.045* DEGREES(ACOS(COS(RADIANS(latpoint))
* COS(RADIANS(latitude))
* COS(RADIANS(longpoint) - RADIANS(longitude))
+ SIN(RADIANS(latpoint))
* SIN(RADIANS(latitude)))) AS distance_in_km
FROM zip
JOIN (
SELECT 42.81 AS latpoint, -70.81 AS longpoint
) AS p ON 1=1
ORDER BY distance_in_km
LIMIT 15
When I execute the above query in sql editor i am getting zip,primary_city, latitude, longitude ,distance_in_km from zip table.
I need to use the same in my JPA repository.I tried using named query but it shows Validation failed exception.Also In my entity class zip.java having only zip, primary_city, latitude, longitude(four attributes).I need to decide how to capture distance_in_km which comes from resultset Kindly help me how to achieve the same in Spring JPA
First you need to set the nativeQuery flag, because this is not a JPQL query.
The SQL query uses named parameters for :latitude
, when there's no such parameter given to the query.
You missed the "+" sign on the 5th line of your query:
+"*COS(RADIANS(longpoint) - RADIANS (height))" +" SIN (RADIANS (latpoint))"
should be:
+"*COS(RADIANS(longpoint) - RADIANS (height))"
+" + SIN (RADIANS (latpoint))"
You query should be written as:
SELECT
code,
name,
length,
height,
111.045* DEGREES (ACOS(COS(RADIANS(latpoint)) * COS (RADIANS(length)) * COS(RADIANS(longpoint) - RADIANS (height)) + SIN (RADIANS (latpoint)) *SIN (RADIANS(length)))) As distance_in_km,
latitude AS latpoint,
longitute AS longpoint
FROM place
ORDER BY distance_in_km
LIMIT 5
Never underestimate the power of code indentation. Some other develop might have to maintain this query, so always write your queries as you want to find them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.