H2 Gis:未找到函数 "WITHIN"
H2 Gis: Function "WITHIN" not found
我有一个集成测试 运行 h2,我尝试使用 jpa 条件进行空间搜索。
测试根据 documentation:
中的要求初始化 h2 gis
@DataJpaTest
@Sql(scripts = "/sql/h2_gis_initialization.sql")
class LocalizationRepositoryTest {
其中 sql 文件包含以下行:
CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load"; CALL H2GIS_SPATIAL();
当运行测试时:
@Test
void it_can_search_within_geometry() {
final Polygon polygon = new GeometryFactory().createPolygon();
List<Localization> results = repository.findAll(
(root, query, builder) -> JTSSpatialPredicates.within(builder, root.get(Localization_.geometry),
polygon));
Assertions.assertEquals(0, results.size());
}
测试失败并出现以下错误:
org.springframework.orm.jpa.JpaSystemException: could not prepare statement; nested exception is
org.hibernate.exception.GenericJDBCException: could not prepare
statement at
my.api.LocalizationRepositoryTest.it_can_search_within_geometry(LocalizationRepositoryTest.java:66)
Caused by: org.hibernate.exception.GenericJDBCException: could not
prepare statement at
my.api.LocalizationRepositoryTest.it_can_search_within_geometry(LocalizationRepositoryTest.java:66)
Caused by: org.h2.jdbc.JdbcSQLException: Function "WITHIN" not found;
SQL statement: select localizati0_.id as id1_48_,
localizati0_.spatial_geometry as spatial_2_48_ from localization_t
localizati0_ where within(localizati0_.spatial_geometry, ?)=?
[90022-197] at
my.api.LocalizationRepositoryTest.it_can_search_within_geometry(LocalizationRepositoryTest.java:66)
出于某种原因,标准试图利用不存在的空间函数。这是为什么?我怎样才能让我的测试工作?
我刚发现另一个类似的问题:How to configure spring-boot project to work with inmemory spatial database for tests?
看来我只是缺少用于测试的 jpa 的方言配置:
我有一个集成测试 运行 h2,我尝试使用 jpa 条件进行空间搜索。
测试根据 documentation:
中的要求初始化 h2 gis@DataJpaTest
@Sql(scripts = "/sql/h2_gis_initialization.sql")
class LocalizationRepositoryTest {
其中 sql 文件包含以下行:
CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load"; CALL H2GIS_SPATIAL();
当运行测试时:
@Test
void it_can_search_within_geometry() {
final Polygon polygon = new GeometryFactory().createPolygon();
List<Localization> results = repository.findAll(
(root, query, builder) -> JTSSpatialPredicates.within(builder, root.get(Localization_.geometry),
polygon));
Assertions.assertEquals(0, results.size());
}
测试失败并出现以下错误:
org.springframework.orm.jpa.JpaSystemException: could not prepare statement; nested exception is org.hibernate.exception.GenericJDBCException: could not prepare statement at my.api.LocalizationRepositoryTest.it_can_search_within_geometry(LocalizationRepositoryTest.java:66) Caused by: org.hibernate.exception.GenericJDBCException: could not prepare statement at my.api.LocalizationRepositoryTest.it_can_search_within_geometry(LocalizationRepositoryTest.java:66) Caused by: org.h2.jdbc.JdbcSQLException: Function "WITHIN" not found; SQL statement: select localizati0_.id as id1_48_, localizati0_.spatial_geometry as spatial_2_48_ from localization_t localizati0_ where within(localizati0_.spatial_geometry, ?)=? [90022-197] at my.api.LocalizationRepositoryTest.it_can_search_within_geometry(LocalizationRepositoryTest.java:66)
出于某种原因,标准试图利用不存在的空间函数。这是为什么?我怎样才能让我的测试工作?
我刚发现另一个类似的问题:How to configure spring-boot project to work with inmemory spatial database for tests?
看来我只是缺少用于测试的 jpa 的方言配置: