HHH-10157 Postgis deserializer accepts also WKT

When the Postgis JDBC driver is on the classpath, Hibernate receives EWKT
instead of EWKB entities.
This commit is contained in:
Karel Maesen 2015-11-29 19:48:02 +01:00
parent 4b99c1b79d
commit b9a988584a
5 changed files with 25 additions and 23 deletions

View File

@ -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

View File

@ -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)

View File

@ -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 ) );

View File

@ -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() );
}
}

View File

@ -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 );
}
}