Spatial
Overview
Hibernate Spatial was originally developed as a generic extension to Hibernate for handling geographic data. Since 5.0, Hibernate Spatial is now part of the Hibernate ORM project, and it allows you to deal with geographic data in a standardized way.
Hibernate Spatial provides a standardized, cross-database interface to geographic data storage and query functions. It supports most of the functions described by the OGC Simple Feature Specification. Supported databases are: Oracle 10g/11g, PostgreSql/PostGIS, MySQL, Microsoft SQL Server and H2/GeoDB.
Spatial data types are not part of the Java standard library, and they are absent from the JDBC specification. Over the years JTS has emerged the de facto standard to fill this gap. JTS is an implementation of the Simple Feature Specification (SFS). Many databases on the other hand implement the SQL/MM - Part 3: Spatial Data specification - a related, but broader specification. The biggest difference is that SFS is limited to 2D geometries in the projected plane (although JTS supports 3D coordinates), whereas SQL/MM supports 2-, 3- or 4-dimensional coordinate spaces.
Hibernate Spatial supports two different geometry models: JTS and
geolatte-geom. As already mentioned, JTS is the de facto
standard. Geolatte-geom (also written by the lead developer of Hibernate Spatial) is a more recent library that
supports many features specified in SQL/MM but not available in JTS (such as support for 4D geometries, and support for extended WKT/WKB formats).
Geolatte-geom also implements encoders/decoders for the database native types. Geolatte-geom has good interoperability with
JTS. Converting a Geolatte geometry
to a JTS `geometry, for instance, doesn’t require copying of the coordinates.
It also delegates spatial processing to JTS.
Whether you use JTS or Geolatte-geom, Hibernate spatial maps the database spatial types to your geometry model of choice. It will, however, always use Geolatte-geom to decode the database native types.
Hibernate Spatial also makes a number of spatial functions available in HQL and in the Criteria Query API. These functions are specified in both SQL/MM as SFS, and are commonly implemented in databases with spatial support (see Hibernate Spatial dialect function support)
Configuration
Hibernate Spatial requires some configuration prior to start using it.
Dependency
You need to include the hibernate-spatial
dependency in your build environment.
For Maven, you need to add the following dependency:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>${hibernate.version}</version>
</dependency>
Dialects
Hibernate Spatial extends the Hibernate ORM dialects so that the spatial functions of the database are made available within HQL and JPQL.
So, for instance, instead of using the PostgreSQL82Dialect
, we use the Hibernate Spatial extension of that dialect which is the PostgisDialect
.
<property
name="hibernate.dialect"
value="org.hibernate.spatial.dialect.postgis.PostgisDialect"
/>
Not all databases support all the functions defined by Hibernate Spatial. The table below provides an overview of the functions provided by each database. If the function is defined in the Simple Feature Specification, the description references the relevant section.
Function |
Description |
PostgresSQL |
Oracle 10g/11g |
MySQL |
SQLServer |
GeoDB (H2) |
Basic functions on Geometry |
||||||
|
SFS §2.1.1.1 |
|||||
|
SFS §2.1.1.1 |
|||||
|
SFS §2.1.1.1 |
|||||
|
SFS §2.1.1.1 |
|||||
|
SFS §2.1.1.1 |
|||||
|
SFS §2.1.1.1 |
|||||
|
SFS §2.1.1.1 |
|||||
|
SFS §2.1.1.1 |
|||||
|
SFS §2.1.1.1 |
|||||
Functions for testing Spatial Relations between geometric objects |
||||||
|
SFS §2.1.1.2 |
|||||
|
SFS §2.1.1.2 |
|||||
|
SFS §2.1.1.2 |
|||||
|
SFS §2.1.1.2 |
|||||
|
SFS §2.1.1.2 |
|||||
|
SFS §2.1.1.2 |
|||||
|
SFS §2.1.1.2 |
|||||
|
SFS §2.1.1.2 |
|||||
|
SFS §2.1.1.2 |
|||||
Functions that support Spatial Analysis |
||||||
|
SFS §2.1.1.3 |
|||||
|
SFS §2.1.1.3 |
|||||
|
SFS §2.1.1.3 |
|||||
|
SFS §2.1.1.3 |
|||||
|
SFS §2.1.1.3 (renamed from union) |
|||||
|
SFS §2.1.1.3 |
|||||
|
SFS §2.1.1.3 |
|||||
Common non-SFS functions |
||||||
|
Returns true if the geometries are within the specified distance of one another |
|||||
|
Returns a new geometry with its coordinates transformed to the SRID referenced by the integer parameter |
|||||
Spatial aggregate Functions |
||||||
|
Returns a bounding box that bounds the set of returned geometries |
- Postgis
-
For Postgis from versions 1.3 and later, the best dialect to use is
org.hibernate.spatial.dialect.postgis.PostgisDialect
.This translates the HQL spatial functions to the Postgis SQL/MM-compliant functions. For older, pre v1.3 versions of Postgis, which are not SQL/MM compliant, the dialect
org.hibernate.spatial.dialect.postgis.PostgisNoSQLMM
is provided.
- MySQL
-
There are several dialects for MySQL:
MySQLSpatialDialect
-
a spatially-extended version of Hibernate
MySQLDialect
MySQL5SpatialDialect
-
a spatially-extended version of Hibernate
MySQL5Dialect
MySQLSpatial56Dialect
-
a spatially-extended version of Hibernate
MySQL55Dialect
.
MySQL versions before 5.6.1 had only limited support for spatial operators. Most operators only took account of the minimum bounding rectangles (MBR) of the geometries, and not the geometries themselves. This changed in version 5.6.1 were MySQL introduced These dialects may therefore produce results that differ from that of the other spatial dialects. For more information, see this page in the MySQL reference guide (esp. the section Functions That Test Spatial Relations Between Geometry Objects) |
- Oracle10g/11g
-
There is currently only one Oracle spatial dialect:
OracleSpatial10gDialect
which extends the Hibernate dialectOracle10gDialect
. This dialect has been tested on both Oracle 10g and Oracle 11g with theSDO_GEOMETRY
spatial database type.This dialect is the only dialect that can be configured using these Hibernate properties:
hibernate.spatial.connection_finder
-
the fully-qualified classname for the Connection finder for this Dialect (see below).
The
ConnectionFinder interfaceThe The default implementation will, when the passed object is not already an In may cases this strategy will suffice.
If not, you can provide your own implementation of this interface on the class path, and configure it in the |
- SQL Server
-
The dialect
SqlServer2008Dialect
supports theGEOMETRY
type in SQL Server 2008 and later.
The |
- GeoDB (H2)
-
The
GeoDBDialect
supports the GeoDB a spatial extension of the H2 in-memory database.
The dialect has been tested with GeoDB version 0.7 |
Types
Hibernate Spatial comes with the following types:
- jts_geometry
-
Handled by
org.hibernate.spatial.JTSGeometryType
it maps a database geometry column type to acom.vividsolutions.jts.geom.Geometry
entity property type. - geolatte_geometry
-
Handled by
org.hibernate.spatial.GeolatteGeometryType
, it maps a database geometry column type to anorg.geolatte.geom.Geometry
entity property type.
It suffices to declare a property as either a JTS or a Geolatte-geom Geometry
and Hibernate Spatial will map it using the
relevant type.
Here is an example using JTS:
import com.vividsolutions.jts.geom.Point;
@Entity(name = "Event")
public static class Event {
@Id
private Long id;
private String name;
private Point location;
//Getters and setters are omitted for brevity
}
We can now treat spatial geometries like any other type.
Event event = new Event();
event.setId( 1L);
event.setName( "Hibernate ORM presentation");
Point point = geometryFactory.createPoint( new Coordinate( 10, 5 ) );
event.setLocation( point );
entityManager.persist( event );
Spatial Dialects defines many query functions that are available both in HQL and JPQL queries. Below we show how we
could use the within
function to find all objects within a given spatial extent or window.
Polygon window = geometryFactory.createPolygon( coordinates );
Event event = entityManager.createQuery(
"select e " +
"from Event e " +
"where within(e.location, :window) = true", Event.class)
.setParameter("window", window)
.getSingleResult();