How can I use JPQL in a NamedQuery to create an en

2019-06-09 12:57发布

问题:

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.

回答1:

I found a way to build the JPQL valid:

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_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)) ) )<= :radius) 
ORDER BY distance

It's weird that I can't access the alias distance in the SELECT-clause but I can in the ORDER BY-clause.