How can I use JPQL in a NamedQuery to create an entity having a calculated transient attribute?

喜你入骨 提交于 2019-12-08 11:33:38

问题


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.



来源:https://stackoverflow.com/questions/26718756/how-can-i-use-jpql-in-a-namedquery-to-create-an-entity-having-a-calculated-trans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!