[GIS] Hibernate Spatial 4 and PostGIS 2.0

hibernate-spatialjavapostgis

I'm having some issues integrating these technologies:

  • Hibernate Spatial 4.0-M1
  • PostGIS 2.0.2 (with compiled JDBC 2.0.2)
  • Hibernate 4.1.1

The specific error is:

Caused by: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.postgis.PGgeometry. Use setObject() with an explicit Types value to specify the type to use.

The entity annotation is:

@NotNull
@Column(columnDefinition="Geometry")
@Type(type="org.hibernate.spatial.GeometryType")
private Point geom;

And the Point creation example is:

Location location = new Location();
WKTReader fromText = new WKTReader();
Point geom = null;
try {
    geom = (Point) fromText.read("POINT(-56.2564083434446 -34.8982159791812)");
} catch (ParseException e) {
    throw new RuntimeException("Not a WKT string:" + "SRID=4326;POINT(-56.2564083434446 -34.8982159791812)");
}
if (!geom.getGeometryType().equals("Point")) {
    throw new RuntimeException("Geometry must be a point. Got a " + geom.getGeometryType());
}
location.setGeom(geom);
locationDAO.insert(location);

Best Answer

I was using Tomcat, and it's connection pooling facilities. I just exposed a datasource to my application through JNDI.

Here's what did work for me:

  • When I included the maven dependency for hibernate-spatial, it has a transitive dependency for hibernate itself, postgresql's jdbc and postgis's jdbc. So what I did was to remove these dependencies (outdated). My pom looks like this:
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-spatial</artifactId>
    <version>4.0</version>
    <exclusions>
        <exclusion>
            <artifactId>hibernate-core</artifactId>
            <groupId>org.hibernate</groupId>
        </exclusion>
        <exclusion>
            <artifactId>postgis-jdbc</artifactId>
            <groupId>org.postgis</groupId>
        </exclusion>
        <exclusion>
            <artifactId>postgresql</artifactId>
            <groupId>postgresql</groupId>
        </exclusion>
    </exclusions>
</dependency>

Postgis jdbc is the extension to postgresql jdbc that you need.

  • Then I cloned postgis repository and compiled their jdbc extension. Just run mvn package in the java/jdbc directory. Read its readme.
  • Then I placed the most recent postgresql-jdbc and the recently compiled postgis jdbc in the tomcat's lib directory
  • On the tomcat's server configuration, I changed the database url to jdbc:postgresql_postGIS://localhost:5432/mydatabase. Notice the postgresql_postGIS part. I also changed the driver class to org.postgis.DriverWrapper. This is a wrapper that registers postgis types with the native jdbc.

Here's my final resource configuration in tomcat:

<Resource auth="Container"
          maxActive="120" maxIdle="10" name="jdbc/myapp"
          username="myuser" password="mypassword"
          poolPreparedStatements="true" type="javax.sql.DataSource" 
          driverClassName="org.postgis.DriverWrapper"
          validatingQuery="select 1"
          url="jdbc:postgresql_postGIS://localhost:5432/myapp"/>

This procedure is generally described in the postgis jdbc's README. So make sure you read it.