Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm having some issues integratings this 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 anotation 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);

Thanks!

share|improve this question
add comment

1 Answer 1

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/workwise2"
          username="workwise" password="51977"
          poolPreparedStatements="true" type="javax.sql.DataSource" 
          driverClassName="org.postgis.DriverWrapper"
          validatingQuery="select 1"
          url="jdbc:postgresql_postGIS://localhost:5432/workwise2"/>

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.