2

我想绑定一个("b_shp")“几何”类型的 PostgreSQL 列。特别是以下查询给出“POLYGON”结果:

SELECT GeometryType(b_shp)   ==>  "POLYGON"

@Column "b_shp"我在@Entity 中找不到正确的注释。

我试过这些注释:

@Column(name="b_shp", columnDefinition="geometry(MultiPolygon,4326)")   
private com.vividsolutions.jts.geom.MultiPolygon b_shp;

和:

@Column(name="b_shp", columnDefinition="geometry")  
private com.vividsolutions.jts.geom.Geometry b_shp;

获得此错误:

ERROR:
javax.ejb.EJBException: java.lang.IllegalStateException: Received object of type org.postgresql.util.PGobject

我在用着:

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

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>5.0.4.Final</version>
        </dependency>

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

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1205-jdbc42</version>
        </dependency>

        <dependency>
            <groupId>org.postgis</groupId>
            <artifactId>postgis-jdbc</artifactId>
            <version>1.3.3</version>
        </dependency>

什么是正确的注释?

4

1 回答 1

1

我相信这不是您的注释的问题。我遇到了同样的错误,并且能够通过不使用我的 wildfly 提供的数据源而是像这样连接到数据库来解决它(persistence.xml):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="org.hibernate.events.jpa" transaction-type="JTA">
   <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/yourdatabase"/>
       <property name="hibernate.connection.username" value="username"/>
       <property name="hibernate.connection.password" value="password"/>
       <property name="hibernate.connection.pool_size" value="5"/>

       <property name="hibernate.show_sql" value="false"/>
       <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>

另外,由于早期的 5.0.x 版本的 hibernate 显然没有正确集成 hibernate-spatial 并且为了避免类路径问题,我将文件 jboss-deployment-structure.xml 添加到我的 META-INF 中:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
         <exclusions>
            <module name="org.hibernate" />
            <module name="org.postgresql" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

这将阻止 Wildfly 提供的 hibernate 被您的部署使用,以便您可以改为为最新的 hibernate 版本(撰写本文时为 5.1.0)添加依赖项。然后,您需要为 hibernate、hibernate-spatial 和 postgresql-jdbc 添加依赖项。

另请注意,hibernate 5 不再需要 @Type 注释。

我已经设法让我的项目使用上面的设置和我的一个具有以下属性/列的实体:

@Column(columnDefinition = "geometry(Point,4326)")
private Point position;

我希望它有帮助,祝你好运!

编辑:

我准备了一个工作示例项目来演示 wf10/hibernate5/postgis 的使用 - 在 github 上查看:

https://github.com/Pulvertoastmann/wf10postgis/

于 2016-03-23T13:31:29.750 回答