I have an SQL statement:
SELECT x.SPEED, (6371 * acos(cos(radians(16.65555)))
* cos(radians(LATITUDE)) * cos(radians(LONGITUDE) - radians(54.55555))
+ sin(radians(16.65555)) * sin(radians(LATITUDE))) AS dist
FROM MY_TABLE x
HAVING dist <= 50
ORDER BY dist
How can I put this into a NamedQuery within a Java entity class in a way that the calculated value is set into this entity as a transient attribute called distance?
For the time being I have tried this:
SELECT vsle,
(:distance_unit * FUNC('acos', FUNC('cos', FUNC('radians', :latitude)) *
FUNC('cos', FUNC('radians', vsle.geoPosition.latitude)) *
FUNC('cos', FUNC('radians', vsle.geoPosition.longitude) - FUNC('radians', :longitude)) +
FUNC('sin', FUNC('radians', :latitude)) * FUNC('sin', FUNC('radians', vsle.geoPosition.latitude)) ) )
AS distance
FROM VehicleStateLogEntity vsle
WHERE (distance <= :radius)
but this fails with a java.lang.NullPointerException. It seems as if the calculated value cannot be accessed via its assigned name distance.