Arturo Volpe

I am software enginner.

Using Hibernate Spatial with Wildfly 9/10

17 Jun 2016 » hibernate

Hibernate Spatial is a project that allows hiberante (and JPA) to use Spatial facilities provided by database vendor’s (like postgis).

If we are using a database provided datasource, we need to add the necessary dependencies to the hibernate module in Wildfly.

Dependencies

First we need hibernate spatial and all of its dependencies, by default, it’s depends on GeoLatte and VididSolutions JTS. Also, geolatte depends on slf4j.

To add this dependencies, we need to downloads the desired jars, for this guide, I will use Hibernate Spatial 5.0.7, JTS 1.13, and Geolatte 1.0.1, as Wildfly 10.0.0.Final currently ships with Hibernate 5.0.7, and JTS 1.13 and Geolatte 1.0.1 are transitive dependencies for Hibernate Spatial 5.0.7.

You can get the jars from:

You can also download the dependencies directly through the Maven CLI:

mvn dependency:copy -Dartifact=org.hibernate:hibernate-spatial:5.0.7.Final:jar -DoutputDirectory=.
mvn dependency:copy -Dartifact=org.geolatte:geolatte-geom:1.0.1:jar -DoutputDirectory=.
mvn dependency:copy -Dartifact=com.vividsolutions:jts:1.13:jar -DoutputDirectory=.

If you are using another version of Hibernate, you need to check the Hibernate pom.xml and use the correct versions.

Installation

Download the three jars to $JBOSS_PATH/modules/system/layers/base/org/hibernate/main, and modify the file module.xml that is in the same path.

In the <resources> tag, add:

<resource-root path="hibernate-spatial-5.0.7.Final.jar"/>
<resource-root path="jts-1.13.jar"/>
<resource-root path="geolatte-geom-1.0.1.jar"/>

And in the dependencies tag, add:

<module name="org.slf4j"/>

Also if you are using postgresql, you need to add in the dependencies tag:

<module name="org.postgresql"/>

And this is all.

Test

Add to your pom:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-spatial</artifactId>
  <scope>provided</scope>
  <version>5.0.7.Final</version>
</dependency>

And create a entity like:

import javax.persistence.*;

import com.vividsolutions.jts.geom.Geometry;


@Entity
public class Place extends BaseEntity {

  @NotNull
  private Geometry location;

   // getters and setters
}

Now, we can create a query like:

SELECT
  distance(p.location, :userLocation)
FROM Place p
ORDER BY
  distance(p.location, :userLocation) ASC

And it will be translated to (if we are using postgresql) to:

SELECT st_distance(place0_.location, ?)
FROM place place0_
ORDER BY st_distance(place0_.location, ?)