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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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 (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;
}
}