【问题标题】:JTS geometry in Hibernate spatial generating " ERROR: function within(geometry, bytea) does not exist"Hibernate 空间中的 JTS 几何生成“错误:(geometry, bytea) 中的函数不存在”
【发布时间】:2021-01-21 23:58:41
【问题描述】:

Hibernate-spatial 5.4.22,hibernate.dialect = org.hibernate.spatial.dialect.postgis.PostgisDialect

一个非常简单的查询:

    import org.locationtech.jts.geom.Geometry;
...
@Query(value = "Select s from #{#entityName} s where within(s.shape, :bounds )= true")
public List<SiteModel> findWithinBounds(Geometry bounds);

边界几何由以下方式生成:

GeometryFactory gf = new GeometryFactory();
Polygon bounds = gf.createPolygon(sc);
bounds.setSRID(4326);
return newSiteService.findWithinBounds(bounds);

但它会产生错误

    select
    sitemodel0_.site_id as site_id1_1_,
    sitemodel0_.accuracy as accuracy2_1_,
    sitemodel0_.comment as comment3_1_,
    sitemodel0_.country_code as country_4_1_,
    sitemodel0_.directions as directio5_1_,
    sitemodel0_.flag as flag6_1_,
    sitemodel0_.height as height7_1_,
    sitemodel0_.h_accuracy as h_accura8_1_,
    sitemodel0_.h_method_id as h_method9_1_,
    sitemodel0_.latitude as latitud10_1_,
    sitemodel0_.longitude as longitu11_1_,
    sitemodel0_.method_id as method_12_1_,
    sitemodel0_.orig_coord as orig_co13_1_,
    sitemodel0_.orig_system_id as orig_sy14_1_,
    sitemodel0_.owner_id as owner_i15_1_,
    sitemodel0_.shape as shape16_1_,
    sitemodel0_.site_name as site_na17_1_ 
from
    sc.site_proposed sitemodel0_ 
where
    within(sitemodel0_.shape, ?)=true
WARN : SQL Error: 0, SQLState: 42883
ERROR: ERROR: function within(geometry, bytea) does not exist

所以似乎发现 postgis 形状字段是几何形状的。 (它是 postgis 几何类型),但无法理解 JTS 几何对象。我见过很多关于反向的问题,但没有看到这个错误。

【问题讨论】:

    标签: java spring-boot postgis hibernate-spatial


    【解决方案1】:

    感谢@Karel Maesen 的提示,我确实让它工作了。我需要放

    hibernate.dialect = org.hibernate.spatial.dialect.postgis.PostgisDialect
    spring.jpa.properties.hibernate.dialect = org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect
    

    进入属性。完成后,空间查询 with 和 dwithin 都可以工作了。

    @Query(value = "Select s from #{#entityName} s where within(s.shape, :bounds )= true")
    public List<SiteModel> findWithinBounds(Geometry bounds);
    
    @Query(value = "Select s from #{#entityName} s where dwithin(s.shape, :point, :distance)= true")
    public List<SiteModel> findCloseTo(Geometry point, double distance);
    

    【讨论】:

      【解决方案2】:

      PostgisDialect 已经被弃用了一段时间。您应该使用一种更新的 Postgis 方言,例如 PostgisPG95Dialect。您应该在 SQL 中看到 st_within() 函数而不是 within()

      【讨论】:

      • 我正在关注 Hibernate-spatial 文档。 docs.jboss.org/hibernate/orm/5.4/userguide/html_single/… "Hibernate Spatial 扩展了 Hibernate ORM 方言,以便在 HQL 和 JPQL 中提供数据库的空间功能。因此,例如,我们不使用 PostgreSQL82Dialect,而是使用该方言的 Hibernate Spatial 扩展,即Postgis 方言。”该文档也使用了 within 而不是 st_within。如果文档已过期,最佳信息来源在哪里?
      • 好的。在属性中试过这个:hibernate.dialect = org.hibernate.spatial.dialect.postgis.PostgisDialect spring.jpa.properties.hibernate.dialect = org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect 现在代码运行 - 查询不起作用,但这可能是一个不同的问题。
      • 是的,HQL/Hibernate函数是within(),但是翻译成PSQL SQL为st_within()
      猜你喜欢
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      相关资源
      最近更新 更多