Spring数据JPA和Geometry类型

Ido*_*ash 4 spring-data-jpa spring-boot

我正在开发一个可以在MySql和MS SQL上运行的应用程序.

我有一个空间的"几何"类型的字段.

通过使用:

 @Column(columnDefinition = "geometry")
 private Point geometry;
Run Code Online (Sandbox Code Playgroud)

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

Hibernate正确创建字段(hbm2ddl).

但是插入任何一点都行不通.我得到:数据截断:无法从发送到GEOMETRY字段的数据中获取几何对象

我使用spring-boot-jpa-starter ..而不是直接休眠.

<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>
Run Code Online (Sandbox Code Playgroud)

问候,Ido

小智 11

您好我已成功映射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>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 我在我的实体上有这个:

    @Column(name = "locationpoint", columnDefinition = "POINT") 
    private Point locationpoint;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我在我的application.properties上有这个:

    # needed for Location domain class
    spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect
    
    Run Code Online (Sandbox Code Playgroud)
  4. 我可以使用这个获得价值:

    locationRepository.findOne((long) 1).getLocationpoint().getX();
    locationRepository.findOne((long) 1).getLocationpoint().getY();
    
    Run Code Online (Sandbox Code Playgroud)

我从Matti Tahvonen的例子中得到了我的解决方案:

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

希望这可以帮助.