Hibernate Spatial与PostGIS的地理类型

时间:2012-06-25 16:27:52

标签: hibernate postgis

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

1 个答案:

答案 0 :(得分:0)

使用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);
    }
}

<强>的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(部分)

@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;
    }
}