views:

142

answers:

3

Does anyone have any experience with creating database agnostic apps in Java, particularly with Hibernate, and simultaneously targeting Oracle and Postgres databases?

In particular I am looking at Oracle Spatial and PostGIS. We want to create a Java based SOA which can be used with both Oracle Spatial and PostGIS back ends. I've used Hibernate with both these databases but never with the intention of targeting both. I can create scenarios where the same code can generate different results depending on which database is used.

It maybe that hibernate can handle this but it would be nice to hear if there are any known problems.

Ken

A: 

Hibernate handles targetting different databases using its Dialect abstraction. You don't need to make any changes to your application itself only the hibernate configuration for each database.

You can either specify in your configuration the dialect to use with your database or allow Hibernate to use the JDBC driver settings to determine the appropriate dialect.

Following the comment received, have you looked at Hibernate Spacial? It is an extension to Hibernate to support geographic data by providing the necessary Hibernate types and dialects.

Mark
Yeah, except both Oracle Spatial and PostGIS are extensions that are not supported by standard dialects and many (I'd say most) of the functions are different enough that they **cannot** be abstracted by registering them as dialect functions. That goes double for DDL stuff.
ChssPly76
A: 

In addition to using Hibernates dialects you will want a database creation management tool like Liquibase which will allow the creation code to be abstracted away from the specific syntax of the different databases.

Paul Keeble
+2  A: 

along with hibernate i can recommend Hibernate Spatial , an extension which supports Mysql, Oracle and Postgre, with their respective GIS extensions.

some pitfalls i encountered:

be aware, the configuration of the dialects was not trivial to do correctly. make sure the dialects are not reconfigured for every statement, as it happened to me.

depending on the features from hibernatespatial you use you might get locked in on a specific version number of hibernate

you can use the criteria api ONLY, hql is not directly supported.

my code using hibernatespatial looks like this:

  if (query.getMaxDistance() != null && query.getCenter() != null) {
        basicCriteria.add(SpatialRestrictions.within("coordinate", GeoidCircleFactory.circle(query.getCenter(), query.getMaxDistance())));
    }

you will suffer from some of the quite dire constraints postgis and others are under. i would recommend to relax some of your application needs to better fit the possibilities of your DB. for example, queries in "angle space" are much easier to do than in "euclidean space".

the code contained in GeoidCircleFactory looks quite scary... :)

Andreas Petersson
Just thinking about geoids makes my brain hurt.Anyway, it looks like we are going down the multiple database route whether I think it's a good idea or not, so I'd better download Hibernate Spatial and get going. Thanks for those pointers .Ken
kenneedham
if you have problems regarding hibernatespatial post on the mailing list. its not very active, but questions seem to get answered in a few days.
Andreas Petersson