Skip to content

Instantly share code, notes, and snippets.

@bjornharrtell
Created July 5, 2012 15:47
Show Gist options
  • Save bjornharrtell/3054462 to your computer and use it in GitHub Desktop.
Save bjornharrtell/3054462 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...

@bjornharrtell
Copy link
Author

Sorry no, It's a bit out of scope. It's supposed to be a plain POJO with JPA annotations. Examples of that is easy to find on google... for example http://www.codemiles.com/jpa/jpa-entity-class-example-t6089.html

@kvngreene
Copy link

If you want this to affect only one web deployment:

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>

Then add to your WEB-INF/lib
hibernate-spatial-4.0-M1.jar
jts-1.12.jar

I would recommend creating a custom postgresql module that uses the postgis jar.

@ValCapri
Copy link

Hello,

Thank you for the how to.
Just would like to add three things.

1 - The Hibernate Spatial can be inside your WEB-INF/lib if you don't put them as modules. If it is as modules, you have to put the Hibernate-Spatial dependencies as provided in your POM.xml using Maven.
2 - It is recommended to create a JNDI datasource via the management console or the XML File of your server (might be standalone.xml). Don't forget to verify that driver-class used is org.postgis.DriverWrapper
3 - You have to add the module org.postgresql as an explicit dependency. You can do this by using MANIFEST.MF file or the jboss-deployment-structure.xml

Example of the jboss-deployment-structure.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>

   <deployment>

      <dependencies>
         <module name="org.postgresql" />
      </dependencies>

   </deployment>

</jboss-deployment-structure>

This was tested using JBoss 7.1.1 Final and JBoss EAP 6.1 Final

Just hope that this comment will help someone.

@arun2arunraj
Copy link

Do you have any sample for mysql ? If yes, Please share me the link too.

@vkasala
Copy link

vkasala commented Nov 9, 2014

Can you please give me a hint how to do this on Wildfly 8.1 on OpenShift with Postgresql 9.2?

@dzolo
Copy link

dzolo commented Dec 12, 2014

Hi Viliam,
full example with WildFly 8.1 is available here: wildfly-arquillian-hibernate-spatial-postgis
Hope it helps.

@RolfWiegand
Copy link

I have made a blog on how to setup Hibernate Spatial with Spring and PostGIS on a Tomcat server http://www.wiegand.dk/wordpress/?p=87 for those interested.

@jdc18
Copy link

jdc18 commented May 18, 2016

Has anybody done this with wildfly 8 or 10 with hibernate spatial 5 or 5.1

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