如何使用PostgreSQL与PGpoint进行地理定位? [英] How to work with PGpoint for Geolocation using PostgreSQL?
问题描述
我找到了很多建议,建议将空间数据与Hibernate一起使用
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.
错误:位置"列的类型为point,但表达式的类型为字符变化
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上下文中使用地理数据的最佳方法是什么
谢谢!祝你有美好的一天.
Thanks! have a good day.
推荐答案
无法直接 保存/更新/获取/ 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屋!