Spring 数据 JPA 和几何类型 [英] Spring data JPA and Geometry type
问题描述
我正在开发一个可以在 MySql 和 MS SQL 上运行的应用程序.
I am developing an application that will run on both MySql and MS SQL.
我有一个用于空间的几何"类型字段.
I have a field that is "geometry" type for spatial.
通过使用:
@Column(columnDefinition = "geometry")
private Point geometry;
(点是 org.springframework.data.geo.Point)
(point is org.springframework.data.geo.Point)
Hibernate 正确创建字段 (hbm2ddl).
Hibernate creates the field properly (hbm2ddl).
但是插入任何点都不起作用.我得到:数据截断:无法从您发送到 GEOMETRY 字段的数据中获取几何对象
But inserting any point does not work. I get : Data truncation: Cannot get geometry object from data you send to the GEOMETRY field
我使用 spring-boot-jpa-starter.. 而不是直接休眠.
I use spring-boot-jpa-starter.. and not direct 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>
问候,伊多
推荐答案
您好,我已经成功地在 JPA 中映射了一个点.这是我所做的:
Hello I have successfully mapped a point in JPA. Here's what I did:
我在 Maven 上有这个:
I have this on 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>
我的实体上有这个:
I have this on my entity:
@Column(name = "locationpoint", columnDefinition = "POINT")
private Point locationpoint;
我的 application.properties 上有这个:
I have this on my application.properties:
# needed for Location domain class
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect
我可以使用这个获得价值:
I can get the value using this:
locationRepository.findOne((long) 1).getLocationpoint().getX();
locationRepository.findOne((long) 1).getLocationpoint().getY();
我的解决方案来自 Matti Tahvonen 的示例:
I based my solution from here Matti Tahvonen's example:
https://github.com/mstahv/spring-boot-spatial-example
希望这会有所帮助.
这篇关于Spring 数据 JPA 和几何类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!