JPA Criteria API。查询带有参数的sql函数调用

时间:2011-04-28 14:43:19

标签: java function jpa parameters criteria

我正在尝试使用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();  

我得到了这个例外:

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]  

看起来像JPA查询构造逻辑忘记用逗号分隔params,还是我自己有办法自己做?

1 个答案:

答案 0 :(得分:0)

看起来您定义的PL / SQL函数需要4个参数,并且您只显示了2个参数。