Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 5.0.1 with PostGIS data stored as bytes instead of spatial types

I'm trying to setup a new project with the following combination of technologies

  • Hibernate + spatial 5.0.1
  • PostgreSQL 9.4 with PostGIS 2.1.8
  • Spring 4.2.1
  • JTS 1.13

Entity. I found that org.hibernate.spatial.GeometryType has been removed, and now appears to be moved to org.geolatte.geom.GeometryType in geo-latte-1.0.jar

@Entity
@Table(name = "AREA")
@NamedQueries(@NamedQuery(name = "findAllAreas", query = "SELECT t FROM Area t"))
public class Area {
    private long id;
    private String info;
    private Geometry location;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    @Column(name = "INFO")
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
    @Column(name = "POLYGON")
    @Type(type = "org.geolatte.geom.GeometryType")
    public Geometry getLocation() {
        return location;
    }
    public void setLocation(Geometry location) {
        this.location = location;
    }

}

Spring setup

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <array>
            <value>org.example.entity</value>
        </array>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect
            </prop>
        </props>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/sadb" />
    <property name="username" value="username" />
    <property name="password" value="password" />
</bean>

Database setup

  • Windows ZIP version
  • Copied in PostGIS installation
  • new PostGIS database and created all the extensions postgis, postgis_topology, fuzzystrmatch, postgis_tiger_geocoder

Result

Am am able to persist and retrieve Area entities without issues. However the type on the column seems is byte data, not a PostGIS spatial type.. Any ideas what am I doing wrong?

sadb=> \d+ area;
                                 Table "public.area"
 Column  |          Type          | Modifiers | Storage  | Stats target | Description
---------+------------------------+-----------+----------+--------------+------------
 id      | bigint                 | not null  | plain    |              |
 info    | character varying(255) |           | extended |              |
 polygon | bytea                  |           | extended |              |
like image 585
Adam Avatar asked Sep 18 '15 08:09

Adam


1 Answers

With Hibernate 5+ you don't need the @Type annotation. Including it seems to cause the error I was seeing. Removing it fixes the issue. The polygon column how has a PostGIS type of geometry.

like image 96
Adam Avatar answered Oct 11 '22 10:10

Adam