Hibernate 5.1与Postgis 2.2几何列映射

Mih*_*ban 9 postgresql hibernate postgis

hibernate 5.1 spatial的文档尚未发布(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;
Run Code Online (Sandbox Code Playgroud)

当columnDefinition设置为"Point"时,我得到"column",the_geom"的类型为point,但表达式的类型为bytea".在hibernate spatial 4文档中,据说版本5+不需要@Type注释,但应该使用什么呢?如何将geom存储为有效的Postgis几何?

Dar*_*rio 0

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

在我的场景中,我管理 spring 类中的所有配置:如此处的“示例 9@Configuration所示,我决定使用如下方法: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);
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我的所有jts几何图形(还有其他几何图形geolatteorg.hibernate.spatial.GeolatteGeometryType都按照我的数据库模型中的声明正确映射。

希望这可以帮助你,

达里奥.