HHH-10157 - Postgis deserializer accepts EWKT

Backport of fix to 5.0 branch.
This commit is contained in:
Karel Maesen 2015-12-01 23:02:23 +01:00
parent 052aad0ed4
commit d3b4ae74aa
4 changed files with 22 additions and 17 deletions

View File

@ -21,8 +21,9 @@ 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([group: 'org.geolatte', name: 'geolatte-geom', version: '1.0.1'])
compile(libraries.dom4j) {
transitive = false

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