Skip to content

Instantly share code, notes, and snippets.

@t0kieu
Forked from bjornharrtell/HOWTO.md
Created July 25, 2012 13:26
Show Gist options
  • Save t0kieu/3176186 to your computer and use it in GitHub Desktop.
Save t0kieu/3176186 to your computer and use it in GitHub Desktop.
How to configure and use JBoss AS 7 with Hibernate Spatial and PostGIS
// Example of stateless EJB managed JAX-RS resource fetching an entity containing Hibernate Spatial geometry
// The good stuff: No manual handling of EntityManager transcations! (the container is doing it for you)
@Path("/entities")
@Stateless
public class Entities {
@PersistenceContext
EntityManager em;
@GET
@Path("/{id}/wkt")
public String getWKT(@PathParam("id") String id) {
Entity entity = em.find(Entity.class, Integer.parseInt(id));
return entity.getGeometry().asText();
}
}

How to configure and use JBoss AS 7 with Hibernate Spatial and PostGIS

This is in the scenario where you want JBoss to handle the datasource and transactions, for example when using injected @PersistenceContext in a @Stateless resource. I like it especially when writing REST services with JAX-RS (example code included below).

Create dir:

/modules/org/postgresql/main

Place files at these locations:

/modules/org/postgresql/main/postgresql-8.3-607.jdbc4.jar
/modules/org/postgresql/main/postgis-1.5.3.jar

Create file /modules/org/postgresql/main/module.xml with content:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="org.postgresql">
    <resources>
        <resource-root path="postgresql-8.3-607.jdbc4.jar"/>
        <resource-root path="postgis-1.5.3.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

Place files at these locations:

/modules/org/hibernate/main/hibernate-spatial-4.0-M1.jar
/modules/org/hibernate/main/jts-1.12.jar

Add this xml fragments to the resources tag in /modules/org/hibernate/main/module.xml:

<resource-root path="hibernate-spatial-4.0-M1.jar"/>
<resource-root path="jts-1.12.jar"/>

Add this xml fragment to the dependencies tag in /modules/org/hibernate/main/module.xml:

<module name="org.postgresql"/>

Finally, add this xml fragment to the drivers tag in your deployment configuration (might be /standalone/configuration/standalone.xml):

<driver name="postgresql" module="org.postgresql">
    <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>

NOTE: Trying to use Hibernate Spatial for a specific deployment in WEB-INF/lib will result in classpath problems which is one reason for this how to...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment