问题描述
我刚刚将一个Geography类型的列添加到Postgres数据库,该数据库由应用程序使用hibernate和hibernate spatial访问。每当我用hibernate访问该列时,都会出现一条错误,提示无法转换org.postgresql.util.PGobject类型的对象。我使用的是较早版本的hibernate spatial,但似乎即使是最新版本也不支持Geography数据类型。这是真的吗,还是我错过了什么?有没有办法解决这个限制?
I just added a column of type Geography to a Postgres database that is accessed by an application using hibernate and hibernate spatial. Whenever I access that column with hibernate I get an error saying "Can't convert object of type org.postgresql.util.PGobject". I was using an older version of hibernate spatial but it seems that even the newest version does not support the Geography datatype. Is this really the case or did I just miss something. Is there a way I can work around this limitation?
推荐答案
我有一个使用Glassfish 3.1.2 + Toplink的类似问题。我解决了它实现SessionCustomizer。我相信您可以对地理数据类型使用相同的说法。
I had a similar issue using Glassfish 3.1.2 + Toplink. And I solved it implementing a SessionCustomizer. I believe that you can use the same aproach for Geograph datatypes.
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);
}
}
persistence.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(部分)
SentenceGPS.java (partial)
@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;
}
}
这篇关于Hibernate Spatial和PostGIS的地理类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!