简体   繁体   中英

Mapping Error on Hibernate Spatial 5.0.4.Final and PostgreSQL geometry column

I want to bind a PostgreSQL column ("b_shp") of type "geometry". In particular the following query give "POLYGON" result:

SELECT GeometryType(b_shp)   ==>  "POLYGON"

I can't find the right annotation for the @Column "b_shp" in my @Entity.

I've tried these annotations:

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

and:

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

obtaining this error:

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

I'm using:

        <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>

What is the right annotation?

I believe this is not a problem with your annotation. I've encountered the same error and was able to solve it by not using the datasource provided by my wildfly and instead connecting to the db like so (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>

Also, because the early 5.0.x versions of hibernate apparently have no proper integration of hibernate-spatial and to avoid classpath problems, I added the file jboss-deployment-structure.xml to my 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>

This will prevent the wildfly provided hibernate to be used by your deployment so that you can instead add a dependency for the most recent hibernate version (5.1.0 as of this writing). You will then need to add dependencies for hibernate, hibernate-spatial and postgresql-jdbc.

Also note that hibernate 5 doesn't require the @Type annotation anymore.

I have managed to get my project working with the settings above and one of my entities featuring the following attribute / column:

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

I hope it helps, good luck!

Edit:

I have prepared a working sample project demonstrating the use of wf10/hibernate5/postgis - check it out on github:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM