postgis 和 h2gis 的 Jpa 查询

Jpa query for both postgis and h2gis

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

我也想为 H2gis 测试这些查询,但这些功能将无法使用。

如何使用 hibernate spatial 弥合这个差距?

我的查询是:

@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>

更新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();

然后点赞

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