Pao*_*ati 4 postgis point java-8 hibernate-spatial spring-boot
在 Spring Boot 项目 Java8 中,使用 hibernate-spatial 和 PostgresDB 9.4
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>5.2.10.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
应用程序属性
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.spatial.dialect.postgis.PostgisPG94Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisPG94Dialect
Run Code Online (Sandbox Code Playgroud)
(我也试过 PostgisPG9Dialect)
我的实体有一个财产
...
import com.vividsolutions.jts.geom.Point;
....
@Column(columnDefinition = "Point")
private Point cityLocation;
Run Code Online (Sandbox Code Playgroud)
如果我用空值保存就可以了,但是如果我输入一个值
setCityLocation(new GeometryFactory().createPoint(new Coordinate(lng, lat));
Run Code Online (Sandbox Code Playgroud)
我有:
PSQLException: ERROR: column "city_location" is of type point but expression is of type bytea You will need to rewrite or cast the expression.
Run Code Online (Sandbox Code Playgroud)
在我的数据库中,我可以看到列定义为
type: point
column size: 2147483647
data type: 1111
num prec radix: 10
char octet length: 2147483647
Run Code Online (Sandbox Code Playgroud)
我快疯了...为什么它不起作用?
更新(它仍然不起作用,我正在收集新信息)
1)我认为问题可能出在数据库的创建上。在我的 application.properties 中,我也有:
spring.jpa.properties.hibernate.hbm2ddl.auto=update
Run Code Online (Sandbox Code Playgroud)
所以模式将通过休眠“自动”更新。
2)我可以直接在数据库上成功运行查询(我使用“Squirrel SQL”作为客户端)
update my_table set city_location = POINT(-13,23) where id = 1
Run Code Online (Sandbox Code Playgroud)
如果我
select city_location from my_table where id = 1
Run Code Online (Sandbox Code Playgroud)
答案是
<Other>
Run Code Online (Sandbox Code Playgroud)
我看不到值......我得到了相同的答案,该记录在点类型内具有空值......
3) 使用查询将值设置为“点”列后,我无法再从表中读取数据,收到异常:
org.geolatte.geom.codec.WktDecodeException : Wrong symbol at position: 1 in Wkt: (-13.0,23.0)
Run Code Online (Sandbox Code Playgroud)
4)我查看了 hibernate-spatial-5.2.10.Final.jar ,我在包 org.hibernate.spatial 中发现了两个名为“geolatte”的类:
GeolatteGeometryJavaTypeDescriptor.class GeolatteGeometryType.class
5)而且(特定于 Squirrel SQL 客户端专家):如果我尝试更改“my_table”中列的值(不是“点”city_location,而是其他列中的任何一个),我会收到类似于我的错误当我尝试插入点值时,在 java 中接收:
Exception seen during check on DB. Exception was:
ERROR: operator does not exist: point = character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
Squirrel 是用 java 制作的..所以我可以接受这个奇怪的事情,可能是它以“错误”的方式编写查询,也许它与我在选择时看到的值有关......
有任何想法吗?
我找到了解决方案!!
需要修复代码,我在另一个 stackoverflow 问题中读到的一个魔术救了我的命。
问题是 db 列的创建方式错误:
在数据库中,列类型应该是几何而 不是点
我从@Column 注释中删除了columnDefinition = "Point"并运行了查询
CREATE EXTENSION postgis;
Run Code Online (Sandbox Code Playgroud)
在我的数据库上按照以下说明进行操作: Postgis 安装:类型“几何”不存在
Krishna Sapkota你是我的新超级英雄!
| 归档时间: |
|
| 查看次数: |
2487 次 |
| 最近记录: |