我正在做一个项目,在那里我将检索用户的经度和纬度。因此,我想将它作为Point存储到数据库中。但是,当我尝试这样做时,我遇到了以下错误:o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: Invalid endian flag value encountered.
我采用向客户端公开的模型,并将其映射如下:
public static Point createPoint(double longitude, double latitude){
GeometryFactory gf = new GeometryFactory();
Coordinate coord = new Coordinate(longitude, latitude );
Point point = gf.createPoint( coord );
return point;
}因此,我将此方法大致如下所示,以将值映射为Point:
createPoint(user.getLocation().getLongitude(), user.getLocation().getLatitude());然后将返回的值存储到数据库中。
我的pom文件有以下依赖项:
<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.4.Final</version>
<exclusions>
<exclusion>
<artifactId>postgresql</artifactId>
<groupId>postgresql</groupId>
</exclusion>
</exclusions>
</dependency>jpa配置:
jpa:
database: POSTGRESQL
open-in-view: false
show-sql: true
hibernate:
ddl-auto: none
dialect: org.hibernate.spatial.dialect.postgis.PostgisDialect
naming:
naming-strategy: org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy列定义:
@Column(name="point")
private com.vividsolutions.jts.geom.Point point;对于如何纠正这个错误,我有什么想法吗?提前谢谢各位。
发布于 2017-02-04 21:51:30
有几种方法可以实现这一点,这完全取决于您希望如何将数据存储在底层数据存储中。
正如注释中所指出的,您可以使用AttributeConverter实现将Point坐标的x、y和z组件存储在由某个神奇字符分隔的单个列中:
public class PointerConverter implements AttributeConverter<Point, String> {
@Override
public String converToDatabaseColumn(Point point) {
// read the x, y, z, construct a string delimited by some character and
// return the value. Hibernate will store this in your column.
}
@Override
public Point convertToEntityAttribute(String value) {
// split the value by the delimiter and construct a Point.
// return the constructed Point to be set in the entity.
}
}这种方法的固有问题之一是,它使得查询Point坐标的各个部分几乎不可能。
如果您发现需要对Point进行查询,并提供构成Point坐标的各种x、y或z值,那么最好考虑:
UserType实现@Embeddable来表示持久性世界中的Point。对于自定义UserType,您需要定义一个新类型,在本例中可能称为PointType,它扩展了UserType,并按如下方式引用它:
@Type(type = "PointType")
@Columns({@Column(name = "X"), @Column(name="Y"), @Column(name="Z"))
private Point point;自定义UserType将处理将点坐标的x、y和z部分映射到适当的列X、Y和Z,反之亦然。
对于@Embeddable解决方案,只需创建自己的JpaPoint类,就可以在几何Point类中传递该类,用于读取x、y和z值,并将它们存储在持久性模型的3个属性中。然后,JpaPoint类还可以公开一个助手方法,允许调用者从可嵌入的JpaPoint生成一个Point:
// ctor example
public JpaPoint(Point point) {
this.x = point.getCoordinates().x;
this.y = point.getCoordinates().y;
this.z = point.getCoordinates().z;
}
// helper method
@Transient
public Point getPoint() {
return new Point( new Coordinates( x, y, z ) );
}https://stackoverflow.com/questions/42044927
复制相似问题