java - PostgreSQL 9.4,在我的 JPA namedQuery 中使用 earth_distance

标签 java postgresql function jpa named-query

我有一个表格站点,其中存储了我的站点信息以及纬度和经度。

ozssc=> \d site;
......
site_latitude         | double precision            | 
site_longitude        | double precision            | 
......

我添加到我的 PostgreSQL 服务器 9.4.3

 CREATE EXTENSION cube; 
 CREATE EXTENSION earthdistance;

所有功能都已添加到我的数据库中,我将功能所有者更改为我的登录用户。

当我尝试将 PSQL 连接到我的服务器并执行时:

ozssc=> select * from site s where earth_box('-28.175613','153.52578399999993',100000) @> ll_to_earth(s.site_latitude,s.site_longitude);
ERROR:  function earth_box(unknown, unknown, integer) does not exist
LINE 1: select * from site s where earth_box('-28.175613','153.52578...
                               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

当我尝试将 earth_box 函数放入 JPA 中的命名查询中时:

@NamedQuery(name = "SiteEntity.findByEarthDistance", query = "SELECT s FROM SiteEntity s WHERE earth_box((ll_to_earth( :positionLatitude, :positionLongitude), :range) @> ll_to_earth(s.siteLatitude,s.siteLongitude))"),

我的 intelliJ 提示没有函数匹配??

当我尝试将我的 JAVAEE 应用程序部署到我的 webLogic 12c 时,我发现错误如下:

Message icon - Error An error occurred during activation of changes, please see the log for details.
Message icon - Error Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing [SELECT s FROM SiteEntity s WHERE earth_box((ll_to_earth( :positionLatitude, :positionLongitude), :range) > ll_to_earth(s.siteLatitude,s.siteLongitude))]. [33, 151] The expression is not a valid conditional expression.
Message icon - Error Substituted for missing class Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd) - org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing [SELECT s FROM SiteEntity s WHERE earth_box((ll_to_earth( :positionLatitude, :positionLongitude), :range) > ll_to_earth(s.siteLatitude,s.siteLongitude))]. [33, 151] The expression is not a valid conditional expression.

我现在迷路了,谁能帮帮我!!

再次编辑

我发现 PostgreSQL 9.4 的 earth_box 函数肯定有一些错误,它不能总是得到正确的结果集,我必须将它更改为 earth_distance 函数并使用多选投影使其正常工作,较新的 namedQuery as :

@NamedQuery(name = "SiteEntity.findBySiteLNR", query = "SELECT s, FUNC('earth_distance', FUNC('ll_to_earth',:positionLatitude, :positionLongitude), FUNC('ll_to_earth',s.siteLatitude,s.siteLongitude)) AS dfcl " +
            "FROM SiteEntity s WHERE dfcl < :range ORDER BY dfcl"),

我没有使用 hibernate,而是使用 EclipseLink 和 JPA 2.1,EJB 容器是 Weblogic 12C,EJB 规范 3.1。

最佳答案

经过大约 4 小时的努力,我找到了解决方案。主要是因为它是来自 JPA 的函数调用,所以必须知道一些技巧。

PSQL 语句为:

select * from site s where earth_box(ll_to_earth(-28.175613,153.52578399999993),100000) @> ll_to_earth(s.site_latitude,s.site_longitude);

JPA namedQuery 为:

@NamedQuery(name = "SiteEntity.findByEarthDistance", query = "SELECT s FROM SiteEntity s WHERE FUNC('earth_box', FUNC('ll_to_earth',:positionLatitude, :positionLongitude), :range) > FUNC('ll_to_earth',s.siteLatitude,s.siteLongitude)"),

JPA 中的嵌套函数调用也可以,谢谢!

关于java - PostgreSQL 9.4,在我的 JPA namedQuery 中使用 earth_distance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31649268/

相关文章:

function - WSO2 ESB 4.8.1 中支持的 XPath 函数

java - 如何在 hibernate 中映射 String[]

java - CPU 未列为使用 Windows 的设备

postgresql - 使用时间跨度查询的问题

带有 Postgresql 适配器的 Ruby activerecord 无法选择 OID

Swift Firebase 在整个应用程序中重用一个函数

函数的 C typedef,如何使用 - 简单示例

java - Spring Boot 安全 OAuth2 : WebSecurityConfigurerAdapter: 302 Redirect to/error

java - 没有链接的类图

postgresql - 引用另一个表中的行 (PostgreSQL)