【问题标题】:Jpa query for both postgis and h2gisJpa 查询 postgis 和 h2gis
【发布时间】:2020-09-30 19:46:39
【问题描述】:

我在 JpaRepository 中有查询,它们使用 ST_MakeEnvelope、ST_DWithin 等 Postgis 函数。这是用于生产代码的。

我还想为 H2gis 测试这些查询,但这些功能将无法工作。

如何使用休眠空间来弥补这一差距?

我的查询如下:

@Query(value = "SELECT * FROM Feature f WHERE geometry && ST_MakeEnvelope(:west, :south, :east, :north, :srid)", nativeQuery = true)

如何让它也与 h2 gis 一起使用?

所以我创建了这个查询:

@Query(value = "SELECT * FROM Feature f where dwithin(f.geometry, :centre, :range)", nativeQuery = true)

但这会引发错误:

Caused by: org.h2.jdbc.JdbcSQLException: Function "DWITHIN" not found;

我有以下依赖项:

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.orbisgis</groupId>
            <artifactId>h2gis-functions</artifactId>
            <version>1.3.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>5.4.17.Final</version>
        </dependency>

【问题讨论】:

  • 你在为空间初始化 h2 db 吗?
  • 是的,我的保存操作运行良好,读取也正常。只有当读取查询使用了空间函数时才会出现问题。
  • 你能用ST_DWithin代替dwithin试试
  • 试过了。这也会引发同样的错误。
  • 我的意思是 h2 gis 扩展功能已初始化?像 CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load";调用 H2GIS_SPATIAL();

标签: hibernate jpa h2 postgis


【解决方案1】:

更新 H2GIS 最新版本。在最新版本中,this 问题已修复。

<dependency>
   <groupId>org.orbisgis</groupId>
   <artifactId>h2gis</artifactId>
   <version>1.5.0</version>
</dependency>

你需要初始化H2GIS扩展

来自doc: 要初始化 H2GIS 扩展,请应用 SQL 语法:

CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load";
CALL H2GIS_SPATIAL();

然后用like

@Query(value = "SELECT * FROM Feature f where ST_DWithin(f.geometry, :centre, :range)", nativeQuery = true)

【讨论】:

  • 找不到这个依赖
  • h2gis.org/news/releases/...on 提供了此页面依赖项,但似乎它们不可用或不兼容。
  • 你对 org.locationtech.jts 的依赖是什么?
  • 你在用com.vividsolutions.jts.geom.Geometry对吗?
  • org.locationtech.jts.geom.Geometry
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多