【问题标题】:Hibernate 5.1 with Postgis 2.2 geometry columns mappingHibernate 5.1 与 Postgis 2.2 几何列映射
【发布时间】:2016-08-13 04:11:36
【问题描述】:

hibernate 5.1 空间的文档尚未发布 (AFAIK),我正在尝试将具有 JST 几何字段的实体保存到 PostgreSQL 9.5 + Postgis 2.2,但没有任何运气。

我还注意到 hibernate-core-5.1.0 中没有 org.hibernate.spatial 包。我尝试了以下注释的变体:

@javax.persistence.Column(name = "the_geom", columnDefinition = "Geometry")
public com.vividsolutions.jts.geom.Geometry geom;

当 columnDefinition 设置为“Point”时,我得到“列“the_geom”的类型是点,但表达式的类型是 bytea”。在 hibernate spatial 4 文档中,据说版本 5+ 不需要 @Type 注释,但应该使用什么来代替?如何将几何存储为有效的 Postgis 几何?

【问题讨论】:

  • 嗨@Mihai 我现在有同样的问题,配置和依赖关系相同。我搜索了一段时间没有任何结果,您是否发现了任何导致问题的原因?
  • 嗨@Dario,由于时间不够,我暂时降级到休眠4.3。我正在等待官方文档可用。

标签: hibernate postgresql postgis


【解决方案1】:

在搜索了一段时间后,我找到了一个适合我需要的解决方案(我希望也是你的)。由于在版本 5 中,所有 jtsgeolatte 几何类型都能够由 hibernate 直接管理,因此您应该配置 hibernate 来管理这些类型。

在我的场景中,我在 spring @Configuration 类中管理所有配置:如 here 的“示例 9”所示,我决定使用 MetadataBuilder 方法作为如下:

@Bean
public static MetadataBuilder metadataBuilder() {

    ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().build();
    MetadataSources sources = new MetadataSources( standardRegistry );
    return sources.getMetadataBuilder();
}

@Bean
public static MetadataBuilder spatialConfiguration() {

    JTSGeometryType jtsGeometryType = new JTSGeometryType(PGGeometryTypeDescriptor.INSTANCE);
    return PersistenceConfiguration.metadataBuilder().applyBasicType(jtsGeometryType);
}

通过这种方式,我的所有jts 几何(还有另一个geolatte 几何org.hibernate.spatial.GeolatteGeometryType)按照我的数据库模型中的声明正确映射。

希望对你有帮助,

达里奥。

【讨论】:

  • 我决定暂时使用hibernate 4.3,我还没有测试你的解决方案。我将在不久的将来某个时候迁移到 5.* 并验证此/接受答案。谢谢。
  • 使用 5.2.3.Final 版本的 hibernate jts 几何图形默认支持,无需额外配置。我使用的是 JTS 1.13 版本。
猜你喜欢
  • 2015-02-21
  • 2013-11-07
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多