Hibernate Spatial PostGis PSQLException列的类型为point,但表达式的类型为bytea

时间:2017-08-28 23:05:14

标签: spring-boot java-8 postgis point hibernate-spatial

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

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

application.properties

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;

如果我用null值保存就可以,但如果我输入值

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)我认为问题可能是db的创建。 在我的application.properties中,我也有:

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

因此架构将由hibernate“自动”更新。

2)我可以直接在db上运行查询(我使用“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)使用查询将值设置为'point'列后,我无法再从表中读取,我收到异常:

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”中的列值(不是'point'noity_location,而是其他列中的任何一列),我会在尝试插入一个点值时收到类似于我在java中调用的错误:< / p>

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.

松鼠是用java制作的......所以我可以接受这个奇怪的东西,可能是以'错误'的方式组成查询,也许它与我在选择时看到的值相关...

任何想法?

1 个答案:

答案 0 :(得分:4)

我找到了解决方案!!

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

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

在数据库中,列类型应为几何 NOT point

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

CREATE EXTENSION postgis;
按照以下说明在我的数据库上

Postgis installation: type "geometry" does not exist

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