November 10, 2012

“PostGIS with Hibernate Spatial and Dropwizard”

Update (Jan 16, 2013): I have created an example bootstrap application on GitHub called dropwizard-postgis-example

Today, I learned that Dropwizard allows you to setup arbitrary hibernate properties in the YAML configuration file.

Hibernate 4.x with Spatial extensions doesn’t always seem to accurately infer that it should be using the PostGIS dialect when you have PostgreSQL + PostGIS (still investigating if that should automatically happen or not.) If it does not infer that it should be using the PostGIS dialect, you can force it to do so by setting the hibernate.dialect property.

Normally you would use an XML file for the configurations but dropwizard-hibernate uses a SessionFactory and loads the configuration from a DatabaseConfiguration entity that’s deserialized from the configuration YAML. Thankfully it is smart enough to allow for arbitrary properties via the database.properties key. This is a working YAML entry for using Hibernate 4.0 with spatial extensions and Dropwizard:

Database entry for setting Hibernate’s dialect


database:
  # the name of your JDBC driver
  driverClass: org.postgresql.Driver
  # the username
  user: username
  # the password
  password: password
  # the JDBC URL
  url: jdbc:postgresql://localhost:5432/gis_database
  properties:
    hibernate.dialect: org.hibernate.spatial.dialect.postgis.PostgisDialect

You will know that it has picked the wrong dialect if you get the following output:

HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect

After setting the hibernate.dialect setting as above, you should see:

HHH000400: Using dialect: org.hibernate.spatial.dialect.postgis.PostgisDialect