Spring data JPA 和 Geometry 类型

2024-05-21

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

我有一个空间“几何”类型的字段。

通过使用:

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

(点是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>

问候, 我愿意


您好,我已成功映射 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. 我的实体上有这个:

    @Column(name = "locationpoint", columnDefinition = "POINT") 
    private Point locationpoint;
    
  3. 我的 application.properties 中有这个:

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

    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/mstahv/spring-boot-spatial-example

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring data JPA 和 Geometry 类型 的相关文章

随机推荐