We're updating the issue view to help you get more done. 

Hibernate Spatial cannot parse boxes from PostGIS


We encountered a problem using the PostGIS function st_extent or extent. Since version 5 Hibernate Spatial seems to be unable to parse boxes and throws the follwoing error:

java.lang.NumberFormatException: For input string: "BO"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at org.geolatte.geom.ByteBuffer.from(ByteBuffer.java:78)
at org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor.toGeometry(PGGeometryTypeDescriptor.java:116)
at org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor$2.doExtract(PGGeometryTypeDescriptor.java:88)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:258)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:254)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:244)
at org.hibernate.loader.hql.QueryLoader.getResultRow(QueryLoader.java:453)

The 'BO' comes from BOX(33321440 5979576.8,33321692.8 5979767.2). In PGGeometryTypeDescriptor#toGeometry it will try to parse the result either as WKT or WKB. As a box is not a valid WKT type it will try to parse it as a WKB. As far as I understand it, we would need a third branch handling boxes specifically.

In our case we had to re-register the function extent as st_extent to make it work with PostGIS 2.0. I know that it is not supported by Hibernate Spatial yet, but as the documentation states the return type of extent in PostGIS 1.3 is a BOX3D. Therefore this problems seems to be more general to me.
The code in question is:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public static Geometry toGeometry(Object object) { if ( object == null ) { return null; } ByteBuffer buffer = null; if ( object instanceof PGobject ) { 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() ); }


Hibernate 5.2.2, PostGIS 2.1



Karel Maesen


Marko Skočibušić




Fix versions

Affects versions