问题描述
我在Postgresql中做了一个函数来计算两个坐标之间的距离。我想在我的项目中使用它,以将函数的结果与用户指定的最大距离进行比较。 我的功能如下:
create or replace function distance(lat double precision,lng double precision,db_lat double precision,db_lng double precision)
returns double precision as $dist$
begin
return
6371 * acos(
cos(radians(lat)) * cos(radians(db_lat))
*
cos(radians(db_lng) - radians(lng))
+
sin(radians(lat))
*
sin(radians(db_lat))
);
end;
$dist$ language plpgsql;
它计算两个坐标之间的距离,一个坐标是用户给定的(设置一个点,他们希望在该点附近查找记录),另一个坐标(使用db_)来自数据库中的现有记录。 当我在pgAdmin中尝试它时,它可以工作,但是我不确定如何在我的java项目中实现它:
public static Specification<Rental> inProximity(Double distance,Double lat,Double lng) {
if (distance == null || lat == null || lng == null) {
return null;
} else {
return (root,query,cb) ->
cb.greaterThanorEqualTo(
distance,cb.function("distance",Double.class,cb.literal(lat),cb.literal(lng),root.get("locationLat"),root.get("locationLng"))
);
}
}
它用红色标记了全部,并且错误显示:Cannot resolve method 'greaterThanorEqualTo(java.lang.Double,javax.persistence.criteria.Expression<T>)'
为什么不将函子的返回标记为Double,以便将两个Double进行比较?
解决方法
哇,那比我预期的要容易得多...
cb.greaterThanOrEqualTo(
distance,cb.function("distance",Double.class,cb.literal(lat),cb.literal(lng),root.get("locationLat"),root.get("locationLng"))
);
应该是
cb.lessThanOrEqualTo(
cb.function("distance",root.get("locationLng")),distance
);
以某种方式我搞砸了订单...