5

在 Spring Boot 项目中,Java8,具有 hibernate-spatial 和 PostgresDB 9.4

    <dependency> 
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>5.2.10.Final</version>
    </dependency>

应用程序属性

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

(我也试过 PostgisPG9Dialect)

我的实体有一个属性

...
import com.vividsolutions.jts.geom.Point;
....
@Column(columnDefinition = "Point")
private Point cityLocation;

如果我用空值保存没关系,但如果我输入一个值

setCityLocation(new GeometryFactory().createPoint(new Coordinate(lng, lat));

我有:

PSQLException: ERROR: column "city_location" is of type point but expression is of type bytea  You will need to rewrite or cast the expression.

在我的数据库中,我可以看到列定义为

type: point
column size: 2147483647
data type: 1111
num prec radix:     10
char octet length: 2147483647

我快疯了... 为什么它不起作用?

更新(它仍然不起作用,我正在收集新信息)

1)我认为问题可能是数据库的创建。在我的 application.properties 我也有:

spring.jpa.properties.hibernate.hbm2ddl.auto=update

因此模式将通过休眠“自动”更新。

2)我可以直接在数据库上成功运行查询(我使用“Squirrel SQL”作为客户端)

update my_table set city_location = POINT(-13,23) where id = 1

如果我

select city_location from my_table where id = 1

答案是

<Other>

我看不到值...对于点类型内具有空值的记录,我得到了相同的答案...

3)通过查询为“点”列设置值后,我无法再从表中读取,我收到异常:

org.geolatte.geom.codec.WktDecodeException : Wrong symbol at position: 1 in Wkt: (-13.0,23.0)

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.

Squirrel 是用 java.. 所以我可以接受这个奇怪的东西,可能是它以“错误”的方式编写查询,也许它与我在选择时看到的值有关......

有任何想法吗?

4

2 回答 2

8

我找到了解决方案!

需要对代码进行修复,而我在另一个 stackoverflow 问题中读到的一个魔术救了我的命。

问题是 db 列的创建方式错误:

在数据库中,列类型应该是几何而 不是

我从 @Column 注释中删除了columnDefinition = "Point"并运行了查询

CREATE EXTENSION postgis;

按照以下说明在我的数据库上: Postgis 安装:类型“几何”不存在

Krishna Sapkota,你是我的新超级英雄!

于 2017-08-30T23:56:03.947 回答
0

只需columnDefinition = "POINT"@Column注释中删除 , 并使用 Point 对象。(即使用默认列定义)

于 2021-12-13T22:11:24.737 回答