From b9a988584a1be5e8f0ff903400a2ca32dd9a0c5e Mon Sep 17 00:00:00 2001 From: Karel Maesen Date: Sun, 29 Nov 2015 19:48:02 +0100 Subject: [PATCH] HHH-10157 Postgis deserializer accepts also WKT When the Postgis JDBC driver is on the classpath, Hibernate receives EWKT instead of EWKB entities. --- .../postgis/resources/hibernate.properties | 4 +--- hibernate-spatial/hibernate-spatial.gradle | 9 +++++---- .../spatial/dialect/h2geodb/GeoDbWkb.java | 4 ++++ .../postgis/PGGeometryTypeDescriptor.java | 17 ++++++++++++----- .../postgis/PostgisExpectationsFactory.java | 14 +++----------- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/hibernate-spatial/databases/postgis/resources/hibernate.properties b/hibernate-spatial/databases/postgis/resources/hibernate.properties index e3db44b898..84f0f80e7d 100644 --- a/hibernate-spatial/databases/postgis/resources/hibernate.properties +++ b/hibernate-spatial/databases/postgis/resources/hibernate.properties @@ -8,7 +8,7 @@ hibernate.test.new_metadata_mappings = true hibernate.dialect org.hibernate.spatial.dialect.postgis.PostgisDialect hibernate.connection.driver_class org.postgresql.Driver -hibernate.connection.url jdbc:postgresql://localhost:5432:hibbrtru +hibernate.connection.url jdbc:postgresql://localhost:5432/hibbrtru hibernate.connection.username hibbrtru hibernate.connection.password hibbrtru @@ -22,5 +22,3 @@ hibernate.max_fetch_depth 5 hibernate.cache.region_prefix hibernate.test hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFactory - - diff --git a/hibernate-spatial/hibernate-spatial.gradle b/hibernate-spatial/hibernate-spatial.gradle index c20609ee1a..db0d450ec4 100644 --- a/hibernate-spatial/hibernate-spatial.gradle +++ b/hibernate-spatial/hibernate-spatial.gradle @@ -21,8 +21,8 @@ mavenPom { dependencies { compile(project(':hibernate-core')) - compile([group: 'postgresql', name: 'postgresql', version: '8.4-701.jdbc4']) - compile([group: 'org.geolatte', name: 'geolatte-geom', version: '1.0']) + compile(['org.postgresql:postgresql:9.4-1200-jdbc41']) + compile([group: 'org.geolatte', name: 'geolatte-geom', version: '1.0.1']) compile(libraries.dom4j) { transitive = false @@ -35,9 +35,10 @@ dependencies { testCompile([group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4']) // testCompile([group: 'com.oracle.jdbc', name: 'ojdbc6', version: '11.1.0.7.0']) // testRuntime([group: 'com.microsoft', name: 'sqljdbc', version: '2.0']) -// testRuntime("org.opengeo:geodb:0.7") + testRuntime("org.opengeo:geodb:0.7") // testRuntime("mysql:mysql-connector-java:5.1.15") -// testRuntime("postgresql:postgresql:8.4-701.jdbc4") +// testRuntime("org.postgresql:postgresql:9.4-1204-jdbc42") +// testRuntime("org.postgis:postgis-jdbc:1.1.6") testCompile(libraries.validation) testCompile(libraries.jandex) testCompile(libraries.classmate) diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/h2geodb/GeoDbWkb.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/h2geodb/GeoDbWkb.java index 23e7586a7a..6fcd9b6467 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/h2geodb/GeoDbWkb.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/h2geodb/GeoDbWkb.java @@ -75,6 +75,10 @@ public class GeoDbWkb { return null; } try { + + if (object instanceof com.vividsolutions.jts.geom.Geometry) { + return JTS.from( (com.vividsolutions.jts.geom.Geometry) object ); + } final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); if ( object instanceof Blob ) { return decoder.decode( toByteBuffer( (Blob) object ) ); diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java index e42f188811..54f0c3ae01 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java @@ -19,6 +19,8 @@ import org.geolatte.geom.Geometry; import org.geolatte.geom.codec.Wkb; import org.geolatte.geom.codec.WkbDecoder; import org.geolatte.geom.codec.WkbEncoder; +import org.geolatte.geom.codec.Wkt; +import org.geolatte.geom.codec.WktDecoder; import org.postgresql.util.PGobject; import org.hibernate.type.descriptor.ValueBinder; @@ -90,17 +92,22 @@ public class PGGeometryTypeDescriptor implements SqlTypeDescriptor { }; } - private Geometry toGeometry(Object object) { + public static Geometry toGeometry(Object object) { if ( object == null ) { return null; } ByteBuffer buffer = null; if ( object instanceof PGobject ) { - buffer = ByteBuffer.from( ( (PGobject) object ).getValue() ); - final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); - return decoder.decode( buffer ); + String pgValue = ((PGobject) object ).getValue(); + if (pgValue.charAt( 0 ) == 'S') { // /we have a Wkt value + final WktDecoder decoder = Wkt.newDecoder( Wkt.Dialect.POSTGIS_EWKT_1 ); + return decoder.decode(pgValue); + } else { + buffer = ByteBuffer.from( pgValue ); + final WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); + return decoder.decode( buffer ); + } } throw new IllegalStateException( "Received object of type " + object.getClass().getCanonicalName() ); - } } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisExpectationsFactory.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisExpectationsFactory.java index 9ee12891fe..e50b2b96bf 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisExpectationsFactory.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/dialects/postgis/PostgisExpectationsFactory.java @@ -18,6 +18,7 @@ import org.postgresql.util.PGobject; import org.jboss.logging.Logger; import org.hibernate.spatial.HSMessageLogger; +import org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor; import org.hibernate.spatial.testing.AbstractExpectationsFactory; import org.hibernate.spatial.testing.DataSourceUtils; import org.hibernate.spatial.testing.NativeSQLStatement; @@ -244,17 +245,8 @@ public class PostgisExpectationsFactory extends AbstractExpectationsFactory { //remove redundancy with toGeometry function in PGGeometryTypeDescriptor @Override protected Geometry decode(Object object) { - ByteBuffer buffer = null; - if (object instanceof PGobject ) { - buffer = ByteBuffer.from( ( (PGobject) object ).getValue() ); - } else if ( object instanceof byte[] ) { - byte[] bytes = (byte[]) object; - ByteBuffer.from( bytes ); - } else { - throw new IllegalStateException( "Received object of type " + object.getClass().getCanonicalName() ); - } - WkbDecoder decoder = Wkb.newDecoder( Wkb.Dialect.POSTGIS_EWKB_1 ); - return JTS.to(decoder.decode( buffer)); + org.geolatte.geom.Geometry geometry = PGGeometryTypeDescriptor.toGeometry( object ); + return JTS.to( geometry ); } }