Spring Data JPA和Geometry类型

7

我正在开发一款应用程序,可以在MySql和MS SQL上运行。

我有一个空间数据类型的“geometry”字段。

通过使用:

 @Column(columnDefinition = "geometry")
 private Point geometry;

(point是org.springframework.data.geo.Point)

Hibernate可以正确创建字段(hbm2ddl)。

但是插入任何点都无法正常工作。 我收到的错误信息是:数据截断:无法从您发送到GEOMETRY字段的数据中获取几何对象

我使用spring-boot-jpa-starter,而不是直接使用Hibernate。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
       <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>5.2.2.Final</version>
        </dependency>

Regards, Ido


你是否指定了 Hibernate Spatial dialect。请参阅 http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#spatial - Anton N
是的,我有。仍然无法工作。 - Ido Barash
你能分享一个 Spring Data Repository 类和一个 Spring Boot 配置属性/ yml 吗? - Anton N
可能你的点应该来自以下包:com.vividsolutions.jts.geom.Point; 参见:http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#spatial-types - Anton N
1个回答

19

你好,我已经成功地在JPA中映射了一个点。以下是我所做的:

  1. 我在Maven上有这个:

    <dependency>
        <groupId>com.vividsolutions</groupId>
        <artifactId>jts</artifactId>
        <version>1.13</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>5.2.5.Final</version>
    </dependency>
    
  2. 我在我的实体上有这个:

  3. @Column(name = "locationpoint", columnDefinition = "POINT") 
    private Point locationpoint;
    
  4. 我在我的application.properties文件中有以下配置:

  5. # needed for Location domain class
    spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect
    
  6. 我可以使用这个获取值:

    locationRepository.findOne((long) 1).getLocationpoint().getX();
    locationRepository.findOne((long) 1).getLocationpoint().getY();
    

我基于Matti Tahvonen的示例解决方案:

https://github.com/mstahv/spring-boot-spatial-example

希望对你有所帮助。


我遇到了这些问题,然后在https://github.com/hbadri1/React_Springboot_Mapbox/blob/master/README.md中收集了我的React Springboot迷你演示。 - Houssam Badri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接