映射到 com.vividsolutions.jts.geom.Point 无效的字节序标志

Mapping to com.vividsolutions.jts.geom.Point invalid endian flag

我正在从事一个项目,我将在其中检索用户的纬度和经度。由此,我想将它作为一个点存储到数据库中。但是,当我尝试这样做时,我 运行 出现以下错误: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;
    }

所以我会大致如下调用这个方法来将值映射为一个点:

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;

关于如何修复此错误的任何想法? 提前谢谢大家。

有几种方法可以实现这一点,这完全取决于您希望数据如何存储在基础数据存储中。

正如评论中所指出的,您可以使用 AttributeConverter 实现来存储 Pointxyz 组件] 的单个列中的坐标由某个魔术字符分隔:

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 并提供构成 xyz 的各种值=17=]的坐标那么你最好考虑:

  • 自定义 UserType 实现
  • 使用 @Embeddable 代表您在持久性世界中的 Point

对于自定义 UserType,您需要定义一个新类型,在本示例中可能称为 PointType,它扩展了 UserType,并像下面这样引用它:

@Type(type = "PointType")
@Columns({@Column(name = "X"), @Column(name="Y"), @Column(name="Z"))
private Point point;

自定义 UserType 将处理将点坐标的 xyz 部分映射到适当的列 XYZ,反之亦然。

对于 @Embeddable 解决方案,您只需创建自己的 JpaPoint class,您可以为其传递几何 Point class读取 xyz 值并将它们存储在持久性模型的 3 个属性中。然后 JpaPoint class 还可以公开一个辅助方法,允许调用者从 JpaPoint 可嵌入的 Point 中生成一个 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 ) );
}