Jpa 使用Hibernate Spatial将多边形几何体持久化到PostgreSQL数据库

Jpa 使用Hibernate Spatial将多边形几何体持久化到PostgreSQL数据库,jpa,hibernate-spatial,Jpa,Hibernate Spatial,我正在学习hibernate spatial,并试图遵循这一点 我正在尝试将多多边形持久化到Postgres数据库 我的数据库脚本: create database multipolygon_test; create extension postgis; create extension postgis_topology; create table test( id bigint ); SELECT AddGeometryColumn ('public','test','the_ge

我正在学习hibernate spatial,并试图遵循这一点

我正在尝试将多多边形持久化到Postgres数据库

我的数据库脚本:

create database multipolygon_test;
create extension postgis;
create extension postgis_topology;
create table test(
    id  bigint
); 
SELECT AddGeometryColumn ('public','test','the_geom',4326,'MULTIPOLYGON',2);
我在项目中使用这些依赖项:

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>4.3</version>
        <exclusions>
            <exclusion>
                <groupId>postgresql</groupId>
                <artifactId>postgresql</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.postgis</groupId>
                <artifactId>postgis-jdbc</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.7.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.7.Final</version>
    </dependency>

    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901.jdbc4</version>
    </dependency>

    <dependency>
        <groupId>org.postgis</groupId>
        <artifactId>postgis-jdbc</artifactId>
        <version>1.3.3</version>
    </dependency>
我的持久性xml

<persistence
    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"
    version="2.0">
    <persistence-unit
        name="org.hibernate.events.jpa"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>test.Test</class>
        <properties>
            <property
                name="hibernate.dialect"
                value="org.hibernate.spatial.dialect.postgis.PostgisDialect" />

            <property
                name="hibernate.connection.driver_class"
                value="org.postgresql.Driver" />
            <property
                name="hibernate.connection.url"
                value="jdbc:postgresql://localhost:5432:multipolygon_test" />
            <property
                name="hibernate.connection.username"
                value="" />
            <property
                name="hibernate.connection.password"
                value="" />
            <property
                name="hibernate.connection.pool_size"
                value="5" />

            <property
                name="hibernate.show_sql"
                value="true" />
            <property
                name="hibernate.format_sql"
                value="true" />

            <property
                name="hibernate.max_fetch_depth"
                value="5" />

            <property
                name="hibernate.hbm2ddl.auto"
                value="update" />

        </properties>
    </persistence-unit>
</persistence>
电话:

public static void main(String[] args) {
        String polygon = "MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))";

        WKTReader fromText = new WKTReader(new GeometryFactory(new PrecisionModel(), 4326));
        Geometry geom = null;
        try {
            geom = fromText.read(polygon);
        } catch (ParseException e) {
            throw new RuntimeException("Not a WKT string:" + polygon);
        }

        EntityManager em = JPAUtil.createEntityManager();

        em.getTransaction().begin();

        Test test = new Test();
        test.setId(1);
        test.setGeometry(((com.vividsolutions.jts.geom.MultiPolygon) geom));
        em.persist(test);
        em.getTransaction().commit();
        em.close();

        JPAUtil.close();

    }
例外情况:

Caused by: org.postgresql.util.PSQLException: Unknown type geometry.
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setPGobject(AbstractJdbc2Statement.java:1603)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1795)
    at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:37)
    at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:46)
    at org.hibernate.spatial.dialect.AbstractJTSGeometryValueBinder.bind(AbstractJTSGeometryValueBinder.java:48)
    at org.hibernate.spatial.dialect.AbstractJTSGeometryValueBinder.bind(AbstractJTSGeometryValueBinder.java:39)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3121)
    ... 12 more
我不知道会出什么问题。我尝试用com.livitsolutions.jts.geom.Geometry、org.postgis.Geometry、org.postgis.MuliPolygon替换MultiPolygon,尝试将列定义指定为“Geometry(MultiPolygon,4326)”,但没有效果


我能想到的唯一一件事是我的依赖性被破坏了,但我也尝试过在hibernate 4.2.0中使用hibernate spatial 4.0.1。这没有帮助。

我偶然发现了这个问题。如果问题仍然存在,请尝试更改:

        <property
            name="hibernate.connection.driver_class"
            value="org.postgresql.Driver" />

进入:


因为您实际上使用的是PostgreSql驱动程序,没有添加空间函数。Postgis驱动程序包装器正在为您添加它们

Caused by: org.postgresql.util.PSQLException: Unknown type geometry.
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setPGobject(AbstractJdbc2Statement.java:1603)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1795)
    at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:37)
    at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:46)
    at org.hibernate.spatial.dialect.AbstractJTSGeometryValueBinder.bind(AbstractJTSGeometryValueBinder.java:48)
    at org.hibernate.spatial.dialect.AbstractJTSGeometryValueBinder.bind(AbstractJTSGeometryValueBinder.java:39)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3121)
    ... 12 more
        <property
            name="hibernate.connection.driver_class"
            value="org.postgresql.Driver" />
        <property
            name="hibernate.connection.driver_class"
            value="org.postgis.DriverWrapper" />