如何使用 PostgreSQL 使用 PGpoint 进行地理定位? [英] How to work with PGpoint for Geolocation using PostgreSQL?

查看:22
本文介绍了如何使用 PostgreSQL 使用 PGpoint 进行地理定位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了很多建议在 Hibernate 中使用空间数据的答案 空间数据地理定位 但我想知道这是否是最好的,因为我发现 PostgreSQL 与 PGpoint 一起用于地理定位.我实现了但它不起作用,因为不保存.

I found a lot of answers that suggest to use spatial data with Hibernate spatial data geolocation but I want to know if that is the best because I found that PostgreSQL works with PGpoint for GeoLocation. I implemented but it doesn't work because doesn't save.

错误:位置"列的类型是点,但表达式的类型是字符变化

ERROR: column "location" is of type point but expression is of type character varying

我有同样的问题,但没有人回答他.如果没有人知道这个问题,那么让我在下面添加其他问题.

I have the same question but nobody answered him. So let me add other question below if nobody knows about this question.

作为建议,我想知道在 Spring Boot Context 上使用地理数据的最佳方法是什么

谢谢!祝你有美好的一天.

Thanks! have a good day.

推荐答案

没有办法直接save/update/get/ PGpoint对象,然后你必须创建你自己的用户类型来支持 PGpoint 以便转换它,在保存之前,UserType 是 Hibernate 的一个类,它允许创建自定义类型以便在保存到数据库之前转换它.以下是您需要实现的代码:

There is no way to save/update/get/ PGpoint object directly, Then you have to create your own user type for supporting PGpoint in order to convert it, before this is saved, UserType is a class of Hibernate which allows to create custom type in order to convert it before to save on database. Here is code that you need to implement:

首先:需要创建一个实现 UserType 的类:

public class PGPointType implements UserType {
    @Override
    public int[] sqlTypes() {
        return new int[]
                {
                        Types.VARCHAR
                };
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Class<PGpoint> returnedClass() {
        return PGpoint.class;
    }

    @Override
    public boolean equals(Object obj, Object obj1) {
        return ObjectUtils.equals(obj, obj1);
    }

    @Override
    public int hashCode(Object obj) {
        return obj.hashCode();
    }

    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws SQLException {
        if (names.length == 1) {
            if (resultSet.wasNull() || resultSet.getObject(names[0]) == null) {
                return null;
            } else {
                return new PGpoint(resultSet.getObject(names[0]).toString());
            }
        }
        return null;
    }


    @Override
    public void nullSafeSet(PreparedStatement statement, Object value, int index, SharedSessionContractImplementor sharedSessionContractImplementor) throws SQLException {
        if (value == null) {
            statement.setNull(index, Types.OTHER);
        } else {
            statement.setObject(index, value, Types.OTHER);
        }
    }

    @Override
    public Object deepCopy(Object obj) {
        return obj;
    }

    @Override
    public boolean isMutable() {
        return Boolean.FALSE;
    }

    @Override
    public Serializable disassemble(Object obj) {
        return (Serializable) obj;
    }

    @Override
    public Object assemble(Serializable serializable, Object obj) {
        return serializable;
    }

    @Override
    public Object replace(Object obj, Object obj1, Object obj2) {
        return obj;
    }

}

第二: 需要在实体头上添加@TypeDef注解,添加一个名字和你创建的PGPointType,在一些PGpoint类型的字段头上,添加@Type注解您创建的名称:

  @TypeDef(name = "type", typeClass = PGPointType.class)
  @Entity
  public class Entity {

       @Type(type = "type")
       private PGpoint pgPoint;

       // Getters and setters 

  }    

亲切的问候.

这篇关于如何使用 PostgreSQL 使用 PGpoint 进行地理定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆