gpt4 book ai didi

使用 PostGIS 的地理类型 hibernate 空间

转载 作者:行者123 更新时间:2023-12-03 17:56:10 25 4
gpt4 key购买 nike

我刚刚向 Postgres 数据库添加了一个 Geography 类型的列,该数据库由使用 hibernate 和 hibernate 空间的应用程序访问。每当我使用 hibernate 访问该列时,都会收到一条错误消息,提示“无法转换 org.postgresql.util.PGobject 类型的对象”。我使用的是旧版本的 hibernate 空间,但似乎即使是最新版本也不支持 Geography 数据类型。真的是这样还​​是我只是错过了什么。有没有办法解决这个限制?

最佳答案

我在使用 Glassfish 3.1.2 + Toplink 时遇到了类似的问题。我解决了它实现一个 SessionCustomizer。我相信您可以对 Geograph 数据类型使用相同的方法。

PostGISJPACustomizer.java

public class PostGISJPACustomizer implements SessionCustomizer {

@Override
public void customize(Session s) throws Exception {
RelationalDescriptor desc01 = (RelationalDescriptor) s.getDescriptor(SentenceGPS.class);
DirectToFieldMapping mapping01 = (DirectToFieldMapping) desc01.getMappingForAttributeName("geom");
mapping01.setConverter(null);

RelationalDescriptor desc02 = (RelationalDescriptor) s.getDescriptor(SentenceLine.class);
DirectToFieldMapping mapping02 = (DirectToFieldMapping) desc02.getMappingForAttributeName("geom");
mapping02.setConverter(null);

RelationalDescriptor desc03 = (RelationalDescriptor) s.getDescriptor(SentencePolygon.class);
DirectToFieldMapping mapping03 = (DirectToFieldMapping) desc03.getMappingForAttributeName("geom");
mapping03.setConverter(null);
}
}

持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="onet-ejbPU" transaction-type="JTA">
<jta-data-source>jdbc/onet</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.session.customizer" value="com.util.PostGISJPACustomizer"/>
</properties>
</persistence-unit>
</persistence>

SentenceGPS.java(部分)
@Entity
public class SentenceGPS implements Serializable {

@Column
private PGgeometry geom;

public void setGeom(LineString geom) {
this.geom = GeoUtils.getPGeo(geom);
}

public LineString getGeom() {
return (LineString) GeoUtils.getJTS(geom);
}
}

GeoUtils.java
public class GeoUtils {

public static PGgeometry getPGeo(Geometry g) throws SQLException {
String PGgeometryStr = "SRID=" + g.getSRID() + ";" + g.toString();
PGgeometry pg = new PGgeometry(PGgeometry.geomFromString(PGgeometryStr));
return pg;
}

// This function needs improvements due peformance issues
public static Geometry getJTS(PGgeometry g) throws ParseException, SQLException {
WKTReader reader = new WKTReader(new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), g.getGeometry().getSrid()));
String geometryString = g.getGeometry().toString();

Geometry geom;
if (geometryString.indexOf(';') != -1) {
String[] temp = PGgeometry.splitSRID(geometryString);
int srid = Integer.parseInt(temp[0].substring(5));
geom = (Geometry) reader.read(temp[1]);
geom.setSRID(srid);
} else {
geom = (Geometry) reader.read(geometryString);
}
return geom;
}
}

关于使用 PostGIS 的地理类型 hibernate 空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11193299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com