JPA Criteria API. Query with sql function call which takes parameters

心不动则不痛 提交于 2021-02-18 06:53:26

问题


I'm trying to construct this query using Criteria typesafe API:

select * from xxx_table xxx where CALC_DISTANCE(xxx.latitude, xxx.longitude, :lat, :lng) < :dist

CALC_DISTANCE definded PL/SQL function:  
FUNCTION calc_distance(  
 pLat1 NUMBER,  
 pLon1 NUMBER,  
 pLat2 NUMBER,  
 pLon2 NUMBER)  
 RETURN NUMBER  


CriteriaBuilder builder = JpaHandle.get().getCriteriaBuilder();  
CriteriaQuery<XXX> criteria = builder.createQuery(XXX.class);  
Root<XXX> xxxRoot = criteria.from(XXX.class);  

ParameterExpression<Double> latParam = builder.parameter( Double.class );  
ParameterExpression<Double> lngParam = builder.parameter( Double.class );  
ParameterExpression<Double> distParam = builder.parameter( Double.class );  

Expression<Double> distFunction = builder.function("CALC_DISTANCE", Double.class, xxxRoot.get(XXX_.latitude), xxxRoot.get(XXX_.longitude), latParam, lngParam);  

criteria.where(builder.greaterThan(distFunction, distParam));  
TypedQuery<XXX> q = em.createQuery(criteria);  
q.setParameter(latParam, latitude);  
q.setParameter(lngParam, longitude);  
q.setParameter(distParam, new Double(distance.toString()));  
return q.getResultList();  

I get this exception:

Caused by: org.hibernate.QueryException: could not resolve property:  
latitudegeneratedAlias0 of: domain.XXX  
[select generatedAlias0 from domain.XXX as generatedAlias0 where  
CALC_DISTANCE(generatedAlias0.latitudegeneratedAlias0.longitude:param0:param1)>:param2]  

Seems like JPA query construction logic forgets to separate params with commas, or do I have somehow to do it myself?


回答1:


It looks like your defined PL/SQL function takes 4 arguments and you only have 2 arguments shown.



来源:https://stackoverflow.com/questions/5820415/jpa-criteria-api-query-with-sql-function-call-which-takes-parameters

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