【问题标题】:Hibernate Spatial 5 - GeometryTypeHibernate Spatial 5 - GeometryType
【发布时间】:2015-10-05 02:08:57
【问题描述】:

将 Hibernate-spatial 升级到版本 5.0.0.CR2 后,以下声明不再起作用:

@Column(columnDefinition = "geometry(Point,4326)")
@Type(type = "org.hibernate.spatial.GeometryType")
private Point position;

有一个:

org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.spatial.GeometryType]

正如我所见,该类不再存在于 Jar-File 中。 GeometryType 发生了什么以及如何替换它?还是要包含另一个 jar 文件?

编辑:澄清。我将 Hibernate-Spatial 与 PostgreSQL-Postgis 数据库结合使用。

【问题讨论】:

    标签: java hibernate postgis hibernate-spatial


    【解决方案1】:

    嗯,解决方案太容易看到了。只需删除@Type 注解,声明如下所示:

    @Column(columnDefinition = "geometry(Point,4326)")
    private Point position;
    

    Source:

    注意@Type 注释。这会通知 Hibernate location 属性是 Geometry 类型。 @Type 注释是 Hibernate 特定的,并且是唯一需要的非 JPA 注释。 (在 Hibernate 的未来版本(版本 5 和更高版本)中,不再需要显式声明几何值属性的类型。)

    【讨论】:

    • Hibernate 文档和其他所有帖子都不适用于 hibernate 5,但您的回答确实有效!谢谢!
    • 我还必须使用正确的GeometryFactory 来创建Point 对象。使用 new GeometryFactory(new PrecisionModel(), 4326) 对我有用。
    【解决方案2】:

    上面 Denis 建议的解决方案在 Hiberate 5 空间和 Mysql 上对我不起作用。但是以下注释对我有用

      @Column(name = "location",columnDefinition="Geometry")
      private Geometry location;
    
    
      @Column(name = "pointlocation",columnDefinition="Point")
      private Point pointlocation;
    

    如果您运行的是 Hibernate 5.3+,则可以跳过列定义

      @Column(name = "location")
      private Geometry location;
    
    
      @Column(name = "pointlocation")
      private Point pointlocation;
    

    【讨论】:

    • 这个,帮我
    【解决方案3】:

    对于 Hibernate Spatial 5.2.x,您只需要实体中的以下内容。

    private Point location;

    您不需要像上面提到的解决方案那样的 columnDefinition 或 Type。

    一些额外的细节并检查以上是否有效

    • 一旦您运行设置并在 mysql 中使用 desc table_name,您应该会看到 geometry 的字段类型。这表明您的休眠到数据库的映射工作正常。
    • 现在尝试从 java 创建一个实体和 repo.save(entity),您的实体应该可以正常保存而没有任何错误。

    如果上述设置不能正常工作,您通常会收到错误提示

    数据截断:无法从您发送到 GEOMETRY 字段的数据中获取几何对象 块引用

    希望能帮助别人节省我浪费的 6 个小时!

    【讨论】:

    • 完美,正是我想要的。上面的其他答案不适用于 Hibernate 5.3,只要我删除 columnDefinition 它就可以完美运行。
    【解决方案4】:

    所以几天来我一直在与这个问题作斗争,问题是我希望数据库类型来自 postgis 类型,以便我对最近邻居等运行有效的距离查询。我终于弄清楚了如何为了做到这一点并想与社区分享,所以我创建了一个包含问题解决方案的演示项目。

    我的设置是一个带有 Postgis、Spring boot jpa、Hibernate 5+、Hibernate-spatial 5+ 的 PostgreSQL 数据库,并且还添加了转换为 rest 输出,这再次需要 jackson 的特殊模块。

    项目位于https://github.com/Wisienkas/springJpaGeo

    您要求的重要代码是这样的:

    @type(type = "jts_geometry")
    private Point point;
    

    【讨论】:

    【解决方案5】:

    显然您的 Hibernate 库也需要使用相同的版本。

    我使用的是 postgis / springboot / hibernate-spatial。

    最初 Hibernate-Spatial 是 @ 5.4.10.Final,即使使用此处的解决方案也无法正常工作。

    然后我查看了我的 maven 依赖项 (hibernate-commons-annotations-5.1.0.Final.jar) 中的 hibernate 版本,并记得在某个地方看到了 hibernate 的版本需要匹配。

    所以我将我的 Hibernate-Spatial 降级到 5.1,以下映射语句在没有更多信息的情况下工作:

    // Geometry Object is from the following import
    import com.vividsolutions.jts.geom.*;
    
    @Column(name="domain_loc")
    private Geometry location;
    

    【讨论】:

    • 你救了我!我刚刚做了一些 spring-boot 升级,它带有休眠升级。所以我只是检查了这些依赖项并将休眠空间降级为 5.1.0.Final。现在可以使用了!
    • 很高兴它也帮助了你!!
    【解决方案6】:

    我正在使用休眠 4.3.6 和休眠空间 5.2.2。上面建议的解决方案对我不起作用。 但是,如果我添加休眠空间 4.3

    ,则以下代码有效
    @Type(type="org.hibernate.spatial.GeometryType")
    private Point location;
    

    【讨论】:

      【解决方案7】:

      对我来说,数据按预期存储在 PostgreSQL 中,但使用 REST API 检索数据失败并出现反序列化错误。 这就是 PostgreSQL 12 和 Spring Boot 对我有用的。

      将其包含在 application.properties 文件中

      spring.jpa.database-platform=org.hibernate.spatial.dialect.postgis.PostgisDialect

      pom.xml 更改

      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>5.4.20.Final</version>
      </dependency>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-spatial</artifactId>
          <version>5.4.20.Final</version>
      </dependency>
      <dependency>
          <groupId>org.n52.jackson</groupId>
          <artifactId>jackson-datatype-jts</artifactId>
          <version>1.2.9</version>
      </dependency>
      

      使用@Configuration 注解创建下面的 Bean

      import org.n52.jackson.datatype.jts.JtsModule;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      public class BeanConfig {
      
          @Bean
          public JtsModule jtsModule() {
              return new JtsModule();
          }
      
      }
      

      在你的@Entity 类中,确保

      import org.n52.jackson.datatype.jts.GeometryDeserializer;
      import org.n52.jackson.datatype.jts.GeometrySerializer;
      
      import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
      import com.fasterxml.jackson.databind.annotation.JsonSerialize;
      import org.locationtech.jts.geom.Point;
          
      @Column(name="the_geom", columnDefinition = "POINT")
      @JsonSerialize(using = GeometrySerializer.class)
      @JsonDeserialize(contentUsing = GeometryDeserializer.class)
      private Point point;
      

      这些更改后,我的 Rest API 以预期格式返回数据

      "point": {
                  "type": "Point",
                  "coordinates": [
                      53.85151,
                      -1.55973
                  ]
              }
      

      【讨论】:

        猜你喜欢
        • 2016-05-31
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 2015-09-18
        • 1970-01-01
        • 1970-01-01
        • 2013-09-20
        • 2013-12-20
        相关资源
        最近更新 更多