SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

shane
Hello,

Hibernate Spatial looks very promising, however I'm having trouble getting it to work with SQL Server. I followed along with the Tutorial, making adjustments because I'm generating my hibernate mappings and POJOs via Hibernate Tools reverse engineering. I have tried several tables containing Geometry columns with simple data. The entry in the mapping looks like this:

        <property name="geometry" type="org.hibernatespatial.GeometryUserType">
            <column name="Geometry" />
        </property>

The property in the POJO ends up like this:

     private Geometry geometry;

which is a import com.vividsolutions.jts.geom.Geometry;

However, when trying to execute an HQL or criteria query, I get the following:

Exception in thread "main" java.lang.IllegalArgumentException: Expected byte array.
at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48)
at org.hibernatespatial.AbstractDBGeometryType.nullSafeGet(AbstractDBGeometryType.java:127)
at org.hibernatespatial.GeometryUserType.nullSafeGet(GeometryUserType.java:172)
at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:128)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2114)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
at org.hibernate.loader.Loader.getRow(Loader.java:1230)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
at org.hibernate.loader.Loader.doQuery(Loader.java:724)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2232)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
at org.hibernate.loader.Loader.list(Loader.java:2124)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
at com.axiomalaska.TestClass.main(TestClass.java:36)

Not sure what's going on here. I've tried changing the POJO geometry type to something more specific like com.vividsolutions.jts.geom.Point or com.vividsolutions.jts.geom.Polygon with no luck. I'm using JTDS, but I also tried using Microsoft's JDBC driver. Any ideas?

Thank you,
Shane

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Reply | Threaded
Open this post in threaded view
|

Re: SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

Karel Maesen
Administrator
Hi Shane,

When it's a Geometry or Geography column, the Microsoft JDBC driver is supposed to return a byte-array. In your case it clearly doesn't. Now I have tested HS against Microsoft JDBC driver version 2.0. What version of the MS driver did you use? I can't vouch for the JTDS driver, they may treat these types of objects differently. Could you check with them how they treat geometries?

Could you try and figure out what is returned by the JDBC driver at the point the IllegalArgumentException is thrown?  Could you also post the HQL?

Regards,

Karel Maesen


On 16 Mar 2011, at 01:06, Shane StClair wrote:

> Hello,
>
> Hibernate Spatial looks very promising, however I'm having trouble getting it to work with SQL Server. I followed along with the Tutorial, making adjustments because I'm generating my hibernate mappings and POJOs via Hibernate Tools reverse engineering. I have tried several tables containing Geometry columns with simple data. The entry in the mapping looks like this:
>
>         <property name="geometry" type="org.hibernatespatial.GeometryUserType">
>             <column name="Geometry" />
>         </property>
>
> The property in the POJO ends up like this:
>
>      private Geometry geometry;
>
> which is a import com.vividsolutions.jts.geom.Geometry;
>
> However, when trying to execute an HQL or criteria query, I get the following:
>
> Exception in thread "main" java.lang.IllegalArgumentException: Expected byte array.
> at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48)
> at org.hibernatespatial.AbstractDBGeometryType.nullSafeGet(AbstractDBGeometryType.java:127)
> at org.hibernatespatial.GeometryUserType.nullSafeGet(GeometryUserType.java:172)
> at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:128)
> at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
> at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2114)
> at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
> at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
> at org.hibernate.loader.Loader.getRow(Loader.java:1230)
> at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
> at org.hibernate.loader.Loader.doQuery(Loader.java:724)
> at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
> at org.hibernate.loader.Loader.doList(Loader.java:2232)
> at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
> at org.hibernate.loader.Loader.list(Loader.java:2124)
> at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
> at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
> at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
> at com.axiomalaska.TestClass.main(TestClass.java:36)
>
> Not sure what's going on here. I've tried changing the POJO geometry type to something more specific like com.vividsolutions.jts.geom.Point or com.vividsolutions.jts.geom.Polygon with no luck. I'm using JTDS, but I also tried using Microsoft's JDBC driver. Any ideas?
>
> Thank you,
> Shane
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Reply | Threaded
Open this post in threaded view
|

Re: SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

shane
Hi Karel,

Thanks very much for your quick response. I tried version 3.0 of Microsoft's driver. I believe that both jTDS and the 3.0 Microsoft driver return geometries as Blobs. If I update convert2JTS in SQLServerGeometryUserType to this, it works:

    public Geometry convert2JTS(Object obj) {
        byte[] raw = null;
        if (obj == null)
            return null;
        if ( obj instanceof byte[] ) {
            raw = (byte[]) obj;
        } else if ( obj instanceof Blob ) {
            Blob blob = (Blob) obj;
            try {
                raw = blob.getBytes( 1, ( (Number) blob.length() ).intValue() );
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            throw new IllegalArgumentException("Expected byte array or BLOB.");
        }

        return Decoders.decode(raw);
    }

With this change, I am able to read objects from the database and call methods on the Geometry java properties.

However, when trying to save an object with a Geometry property I'm getting the following error:

org.hibernate.exception.DataException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.axiomalaska.adfg.aerialsurvey.util.HibernateUtil.commitTransaction(HibernateUtil.java:89)
at com.axiomalaska.adfg.aerialsurvey.spatial.EncodeDistrictPolygons.main(EncodeDistrictPolygons.java:30)
Caused by: java.sql.BatchUpdateException: Operand type clash: image is incompatible with geometry
at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 9 more

This is with the jTDS driver. I'm guessing that this is a problem with the driver and not with HS. I don't see geometry listed under jTDS' supported datatypes, so the driver may not know how to properly update geometry fields. I'll try with the Microsoft drivers and possibly ask on the jTDS mailing list.

Thanks for your help.
Shane

On Tue, Mar 15, 2011 at 4:30 PM, Karel Maesen <[hidden email]> wrote:
Hi Shane,

When it's a Geometry or Geography column, the Microsoft JDBC driver is supposed to return a byte-array. In your case it clearly doesn't. Now I have tested HS against Microsoft JDBC driver version 2.0. What version of the MS driver did you use? I can't vouch for the JTDS driver, they may treat these types of objects differently. Could you check with them how they treat geometries?

Could you try and figure out what is returned by the JDBC driver at the point the IllegalArgumentException is thrown?  Could you also post the HQL?

Regards,

Karel Maesen


On 16 Mar 2011, at 01:06, Shane StClair wrote:

> Hello,
>
> Hibernate Spatial looks very promising, however I'm having trouble getting it to work with SQL Server. I followed along with the Tutorial, making adjustments because I'm generating my hibernate mappings and POJOs via Hibernate Tools reverse engineering. I have tried several tables containing Geometry columns with simple data. The entry in the mapping looks like this:
>
>         <property name="geometry" type="org.hibernatespatial.GeometryUserType">
>             <column name="Geometry" />
>         </property>
>
> The property in the POJO ends up like this:
>
>      private Geometry geometry;
>
> which is a import com.vividsolutions.jts.geom.Geometry;
>
> However, when trying to execute an HQL or criteria query, I get the following:
>
> Exception in thread "main" java.lang.IllegalArgumentException: Expected byte array.
>       at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48)
>       at org.hibernatespatial.AbstractDBGeometryType.nullSafeGet(AbstractDBGeometryType.java:127)
>       at org.hibernatespatial.GeometryUserType.nullSafeGet(GeometryUserType.java:172)
>       at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:128)
>       at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
>       at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2114)
>       at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
>       at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
>       at org.hibernate.loader.Loader.getRow(Loader.java:1230)
>       at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
>       at org.hibernate.loader.Loader.doQuery(Loader.java:724)
>       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
>       at org.hibernate.loader.Loader.doList(Loader.java:2232)
>       at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
>       at org.hibernate.loader.Loader.list(Loader.java:2124)
>       at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
>       at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
>       at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
>       at com.axiomalaska.TestClass.main(TestClass.java:36)
>
> Not sure what's going on here. I've tried changing the POJO geometry type to something more specific like com.vividsolutions.jts.geom.Point or com.vividsolutions.jts.geom.Polygon with no luck. I'm using JTDS, but I also tried using Microsoft's JDBC driver. Any ideas?
>
> Thank you,
> Shane
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users



--
Shane StClair
Software Engineer
Axiom Consulting & Design
523 W 8th Ave
Suite 104
Anchorage, AK 99501
Website: http://www.axiomalaska.com

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Reply | Threaded
Open this post in threaded view
|

Re: SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

shane
Did some experimenting. The Microsoft JDBC 2.0 driver is able to read and update geometry data, but it isn't sufficient for reverse engineering. Reverse engineering via Hibernate Tools results in many fields being typed to Serializable, which breaks the existing app. I would have to specify the datatype of each field manually in the reverse engineering file, which mostly defeats the point of reverse engineering.

jTDS still fails to update with the "Operand type clash: image is incompatible with geometry" error. I have a post asking for clarification on the jTDS help forum...

I can't properly test Microsoft's 3.0 driver against the HS 1.1-SNAPSHOT because it uses Hibernate 3.6.0.Final and my app needs Hibernate 3.3.2.GA due to the use of Hibernate Tools (Hibernate Tools isn't compatible with 3.6.0). It's choking on line 58 of SqlServerSpatialDialect, where the new CustomType is created. I understand that this is due to a constructor change in Hibernate. It seems like I should be able to change this to the following:

CustomType geomType = new CustomType(new SQLServerGeometryUserType(), null );

but it's still giving me this error:

java.lang.NoSuchMethodError: org.hibernate.type.CustomType.<init>(Lorg/hibernate/usertype/UserType;[Ljava/lang/String;)V
        at org.hibernatespatial.sqlserver.SQLServerSpatialDialect.<init>(SQLServerSpatialDialect.java:59)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
...etc

Not sure why I'm getting this when using the MS 3.0 driver and not jTDS or MS 2.0.

Unless you have any advice, it looks like I'm currently limited to the MS 2.0 driver, which won't work for this app. Thanks for the help.
-Shane

On Wed, Mar 16, 2011 at 1:05 PM, Shane StClair <[hidden email]> wrote:
Hi Karel,

Thanks very much for your quick response. I tried version 3.0 of Microsoft's driver. I believe that both jTDS and the 3.0 Microsoft driver return geometries as Blobs. If I update convert2JTS in SQLServerGeometryUserType to this, it works:

    public Geometry convert2JTS(Object obj) {
        byte[] raw = null;
        if (obj == null)
            return null;
        if ( obj instanceof byte[] ) {
            raw = (byte[]) obj;
        } else if ( obj instanceof Blob ) {
            Blob blob = (Blob) obj;
            try {
                raw = blob.getBytes( 1, ( (Number) blob.length() ).intValue() );
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            throw new IllegalArgumentException("Expected byte array or BLOB.");
        }

        return Decoders.decode(raw);
    }

With this change, I am able to read objects from the database and call methods on the Geometry java properties.

However, when trying to save an object with a Geometry property I'm getting the following error:

org.hibernate.exception.DataException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.axiomalaska.adfg.aerialsurvey.util.HibernateUtil.commitTransaction(HibernateUtil.java:89)
at com.axiomalaska.adfg.aerialsurvey.spatial.EncodeDistrictPolygons.main(EncodeDistrictPolygons.java:30)
Caused by: java.sql.BatchUpdateException: Operand type clash: image is incompatible with geometry
at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 9 more

This is with the jTDS driver. I'm guessing that this is a problem with the driver and not with HS. I don't see geometry listed under jTDS' supported datatypes, so the driver may not know how to properly update geometry fields. I'll try with the Microsoft drivers and possibly ask on the jTDS mailing list.

Thanks for your help.
Shane

On Tue, Mar 15, 2011 at 4:30 PM, Karel Maesen <[hidden email]> wrote:
Hi Shane,

When it's a Geometry or Geography column, the Microsoft JDBC driver is supposed to return a byte-array. In your case it clearly doesn't. Now I have tested HS against Microsoft JDBC driver version 2.0. What version of the MS driver did you use? I can't vouch for the JTDS driver, they may treat these types of objects differently. Could you check with them how they treat geometries?

Could you try and figure out what is returned by the JDBC driver at the point the IllegalArgumentException is thrown?  Could you also post the HQL?

Regards,

Karel Maesen


On 16 Mar 2011, at 01:06, Shane StClair wrote:

> Hello,
>
> Hibernate Spatial looks very promising, however I'm having trouble getting it to work with SQL Server. I followed along with the Tutorial, making adjustments because I'm generating my hibernate mappings and POJOs via Hibernate Tools reverse engineering. I have tried several tables containing Geometry columns with simple data. The entry in the mapping looks like this:
>
>         <property name="geometry" type="org.hibernatespatial.GeometryUserType">
>             <column name="Geometry" />
>         </property>
>
> The property in the POJO ends up like this:
>
>      private Geometry geometry;
>
> which is a import com.vividsolutions.jts.geom.Geometry;
>
> However, when trying to execute an HQL or criteria query, I get the following:
>
> Exception in thread "main" java.lang.IllegalArgumentException: Expected byte array.
>       at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48)
>       at org.hibernatespatial.AbstractDBGeometryType.nullSafeGet(AbstractDBGeometryType.java:127)
>       at org.hibernatespatial.GeometryUserType.nullSafeGet(GeometryUserType.java:172)
>       at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:128)
>       at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
>       at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2114)
>       at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
>       at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
>       at org.hibernate.loader.Loader.getRow(Loader.java:1230)
>       at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
>       at org.hibernate.loader.Loader.doQuery(Loader.java:724)
>       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
>       at org.hibernate.loader.Loader.doList(Loader.java:2232)
>       at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
>       at org.hibernate.loader.Loader.list(Loader.java:2124)
>       at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
>       at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
>       at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
>       at com.axiomalaska.TestClass.main(TestClass.java:36)
>
> Not sure what's going on here. I've tried changing the POJO geometry type to something more specific like com.vividsolutions.jts.geom.Point or com.vividsolutions.jts.geom.Polygon with no luck. I'm using JTDS, but I also tried using Microsoft's JDBC driver. Any ideas?
>
> Thank you,
> Shane
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users



--
Shane StClair
Software Engineer
Axiom Consulting & Design
523 W 8th Ave
Suite 104
Anchorage, AK 99501
Website: http://www.axiomalaska.com



--
Shane StClair
Software Engineer
Axiom Consulting & Design
523 W 8th Ave
Suite 104
Anchorage, AK 99501
Website: http://www.axiomalaska.com

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Reply | Threaded
Open this post in threaded view
|

Re: SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

shane
Sorry to triple post, but I noticed that I didn't clearly communicate the CustomType error. When using null for the second argument (as shown in the last post), I get the error when reverse engineering in my app, which uses Hibernate 3.3.2.GA, so the problem is happening with that line when run against Hibernate 3.3.2.GA.

On Wed, Mar 16, 2011 at 4:01 PM, Shane StClair <[hidden email]> wrote:
Did some experimenting. The Microsoft JDBC 2.0 driver is able to read and update geometry data, but it isn't sufficient for reverse engineering. Reverse engineering via Hibernate Tools results in many fields being typed to Serializable, which breaks the existing app. I would have to specify the datatype of each field manually in the reverse engineering file, which mostly defeats the point of reverse engineering.

jTDS still fails to update with the "Operand type clash: image is incompatible with geometry" error. I have a post asking for clarification on the jTDS help forum...

I can't properly test Microsoft's 3.0 driver against the HS 1.1-SNAPSHOT because it uses Hibernate 3.6.0.Final and my app needs Hibernate 3.3.2.GA due to the use of Hibernate Tools (Hibernate Tools isn't compatible with 3.6.0). It's choking on line 58 of SqlServerSpatialDialect, where the new CustomType is created. I understand that this is due to a constructor change in Hibernate. It seems like I should be able to change this to the following:

CustomType geomType = new CustomType(new SQLServerGeometryUserType(), null );

but it's still giving me this error:

java.lang.NoSuchMethodError: org.hibernate.type.CustomType.<init>(Lorg/hibernate/usertype/UserType;[Ljava/lang/String;)V
        at org.hibernatespatial.sqlserver.SQLServerSpatialDialect.<init>(SQLServerSpatialDialect.java:59)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
...etc

Not sure why I'm getting this when using the MS 3.0 driver and not jTDS or MS 2.0.

Unless you have any advice, it looks like I'm currently limited to the MS 2.0 driver, which won't work for this app. Thanks for the help.
-Shane

On Wed, Mar 16, 2011 at 1:05 PM, Shane StClair <[hidden email]> wrote:
Hi Karel,

Thanks very much for your quick response. I tried version 3.0 of Microsoft's driver. I believe that both jTDS and the 3.0 Microsoft driver return geometries as Blobs. If I update convert2JTS in SQLServerGeometryUserType to this, it works:

    public Geometry convert2JTS(Object obj) {
        byte[] raw = null;
        if (obj == null)
            return null;
        if ( obj instanceof byte[] ) {
            raw = (byte[]) obj;
        } else if ( obj instanceof Blob ) {
            Blob blob = (Blob) obj;
            try {
                raw = blob.getBytes( 1, ( (Number) blob.length() ).intValue() );
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            throw new IllegalArgumentException("Expected byte array or BLOB.");
        }

        return Decoders.decode(raw);
    }

With this change, I am able to read objects from the database and call methods on the Geometry java properties.

However, when trying to save an object with a Geometry property I'm getting the following error:

org.hibernate.exception.DataException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.axiomalaska.adfg.aerialsurvey.util.HibernateUtil.commitTransaction(HibernateUtil.java:89)
at com.axiomalaska.adfg.aerialsurvey.spatial.EncodeDistrictPolygons.main(EncodeDistrictPolygons.java:30)
Caused by: java.sql.BatchUpdateException: Operand type clash: image is incompatible with geometry
at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 9 more

This is with the jTDS driver. I'm guessing that this is a problem with the driver and not with HS. I don't see geometry listed under jTDS' supported datatypes, so the driver may not know how to properly update geometry fields. I'll try with the Microsoft drivers and possibly ask on the jTDS mailing list.

Thanks for your help.
Shane

On Tue, Mar 15, 2011 at 4:30 PM, Karel Maesen <[hidden email]> wrote:
Hi Shane,

When it's a Geometry or Geography column, the Microsoft JDBC driver is supposed to return a byte-array. In your case it clearly doesn't. Now I have tested HS against Microsoft JDBC driver version 2.0. What version of the MS driver did you use? I can't vouch for the JTDS driver, they may treat these types of objects differently. Could you check with them how they treat geometries?

Could you try and figure out what is returned by the JDBC driver at the point the IllegalArgumentException is thrown?  Could you also post the HQL?

Regards,

Karel Maesen


On 16 Mar 2011, at 01:06, Shane StClair wrote:

> Hello,
>
> Hibernate Spatial looks very promising, however I'm having trouble getting it to work with SQL Server. I followed along with the Tutorial, making adjustments because I'm generating my hibernate mappings and POJOs via Hibernate Tools reverse engineering. I have tried several tables containing Geometry columns with simple data. The entry in the mapping looks like this:
>
>         <property name="geometry" type="org.hibernatespatial.GeometryUserType">
>             <column name="Geometry" />
>         </property>
>
> The property in the POJO ends up like this:
>
>      private Geometry geometry;
>
> which is a import com.vividsolutions.jts.geom.Geometry;
>
> However, when trying to execute an HQL or criteria query, I get the following:
>
> Exception in thread "main" java.lang.IllegalArgumentException: Expected byte array.
>       at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48)
>       at org.hibernatespatial.AbstractDBGeometryType.nullSafeGet(AbstractDBGeometryType.java:127)
>       at org.hibernatespatial.GeometryUserType.nullSafeGet(GeometryUserType.java:172)
>       at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:128)
>       at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
>       at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2114)
>       at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
>       at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
>       at org.hibernate.loader.Loader.getRow(Loader.java:1230)
>       at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
>       at org.hibernate.loader.Loader.doQuery(Loader.java:724)
>       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
>       at org.hibernate.loader.Loader.doList(Loader.java:2232)
>       at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
>       at org.hibernate.loader.Loader.list(Loader.java:2124)
>       at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
>       at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
>       at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
>       at com.axiomalaska.TestClass.main(TestClass.java:36)
>
> Not sure what's going on here. I've tried changing the POJO geometry type to something more specific like com.vividsolutions.jts.geom.Point or com.vividsolutions.jts.geom.Polygon with no luck. I'm using JTDS, but I also tried using Microsoft's JDBC driver. Any ideas?
>
> Thank you,
> Shane
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users



--
Shane StClair
Software Engineer
Axiom Consulting & Design
523 W 8th Ave
Suite 104
Anchorage, AK 99501
Website: http://www.axiomalaska.com



--
Shane StClair
Software Engineer
Axiom Consulting & Design
523 W 8th Ave
Suite 104
Anchorage, AK 99501
Website: http://www.axiomalaska.com



--
Shane StClair
Software Engineer
Axiom Consulting & Design
523 W 8th Ave
Suite 104
Anchorage, AK 99501
Website: http://www.axiomalaska.com

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Reply | Threaded
Open this post in threaded view
|

Re: SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

Karel Maesen
Administrator
Hello Shane,

In the Hibernate 3.6 the CustomType construcor needs to passed a UserType instance where in earlier releases a UserType Class. So you're limited to test against version 1 (or 1.0.1) of Hibernate Spatial. You can check out the code from the tags directory. There are virtually no changes in the SQL Server support between version 1.0 and 1.1.

You could modify the 1.1 code to have
        CustomType geomType = new CustomType(SQLServerGeometryUserType.class, null );
to make it compatible with hibernate 3.3.2.GA.

Can you figure out how Hibernate Tools determines the type? If I find the time, I will also have a look at it.

Regards,

Karel

On 17 Mar 2011, at 01:04, Shane StClair wrote:

> Sorry to triple post, but I noticed that I didn't clearly communicate the CustomType error. When using null for the second argument (as shown in the last post), I get the error when reverse engineering in my app, which uses Hibernate 3.3.2.GA, so the problem is happening with that line when run against Hibernate 3.3.2.GA.
>
> On Wed, Mar 16, 2011 at 4:01 PM, Shane StClair <[hidden email]> wrote:
> Did some experimenting. The Microsoft JDBC 2.0 driver is able to read and update geometry data, but it isn't sufficient for reverse engineering. Reverse engineering via Hibernate Tools results in many fields being typed to Serializable, which breaks the existing app. I would have to specify the datatype of each field manually in the reverse engineering file, which mostly defeats the point of reverse engineering.
>
> jTDS still fails to update with the "Operand type clash: image is incompatible with geometry" error. I have a post asking for clarification on the jTDS help forum...
>
> I can't properly test Microsoft's 3.0 driver against the HS 1.1-SNAPSHOT because it uses Hibernate 3.6.0.Final and my app needs Hibernate 3.3.2.GA due to the use of Hibernate Tools (Hibernate Tools isn't compatible with 3.6.0). It's choking on line 58 of SqlServerSpatialDialect, where the new CustomType is created. I understand that this is due to a constructor change in Hibernate. It seems like I should be able to change this to the following:
>
> CustomType geomType = new CustomType(new SQLServerGeometryUserType(), null );
>
> but it's still giving me this error:
>
> java.lang.NoSuchMethodError: org.hibernate.type.CustomType.<init>(Lorg/hibernate/usertype/UserType;[Ljava/lang/String;)V
>         at org.hibernatespatial.sqlserver.SQLServerSpatialDialect.<init>(SQLServerSpatialDialect.java:59)
>         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
> ...etc
>
> Not sure why I'm getting this when using the MS 3.0 driver and not jTDS or MS 2.0.
>
> Unless you have any advice, it looks like I'm currently limited to the MS 2.0 driver, which won't work for this app. Thanks for the help.
> -Shane
>
> On Wed, Mar 16, 2011 at 1:05 PM, Shane StClair <[hidden email]> wrote:
> Hi Karel,
>
> Thanks very much for your quick response. I tried version 3.0 of Microsoft's driver. I believe that both jTDS and the 3.0 Microsoft driver return geometries as Blobs. If I update convert2JTS in SQLServerGeometryUserType to this, it works:
>
>     public Geometry convert2JTS(Object obj) {
>         byte[] raw = null;
>         if (obj == null)
>             return null;
>         if ( obj instanceof byte[] ) {
>             raw = (byte[]) obj;
>         } else if ( obj instanceof Blob ) {
>             Blob blob = (Blob) obj;
>             try {
>                 raw = blob.getBytes( 1, ( (Number) blob.length() ).intValue() );
>             } catch (SQLException e) {
>                 e.printStackTrace();
>             }
>         } else {
>             throw new IllegalArgumentException("Expected byte array or BLOB.");
>         }
>
>         return Decoders.decode(raw);
>     }
>
> With this change, I am able to read objects from the database and call methods on the Geometry java properties.
>
> However, when trying to save an object with a Geometry property I'm getting the following error:
>
> org.hibernate.exception.DataException: Could not execute JDBC batch update
> at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
> at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
> at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
> at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
> at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
> at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
> at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
> at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
> at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
> at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
> at com.axiomalaska.adfg.aerialsurvey.util.HibernateUtil.commitTransaction(HibernateUtil.java:89)
> at com.axiomalaska.adfg.aerialsurvey.spatial.EncodeDistrictPolygons.main(EncodeDistrictPolygons.java:30)
> Caused by: java.sql.BatchUpdateException: Operand type clash: image is incompatible with geometry
> at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
> at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
> at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
> ... 9 more
>
> This is with the jTDS driver. I'm guessing that this is a problem with the driver and not with HS. I don't see geometry listed under jTDS' supported datatypes, so the driver may not know how to properly update geometry fields. I'll try with the Microsoft drivers and possibly ask on the jTDS mailing list.
>
> Thanks for your help.
> Shane
>
> On Tue, Mar 15, 2011 at 4:30 PM, Karel Maesen <[hidden email]> wrote:
> Hi Shane,
>
> When it's a Geometry or Geography column, the Microsoft JDBC driver is supposed to return a byte-array. In your case it clearly doesn't. Now I have tested HS against Microsoft JDBC driver version 2.0. What version of the MS driver did you use? I can't vouch for the JTDS driver, they may treat these types of objects differently. Could you check with them how they treat geometries?
>
> Could you try and figure out what is returned by the JDBC driver at the point the IllegalArgumentException is thrown?  Could you also post the HQL?
>
> Regards,
>
> Karel Maesen
>
>
> On 16 Mar 2011, at 01:06, Shane StClair wrote:
>
> > Hello,
> >
> > Hibernate Spatial looks very promising, however I'm having trouble getting it to work with SQL Server. I followed along with the Tutorial, making adjustments because I'm generating my hibernate mappings and POJOs via Hibernate Tools reverse engineering. I have tried several tables containing Geometry columns with simple data. The entry in the mapping looks like this:
> >
> >         <property name="geometry" type="org.hibernatespatial.GeometryUserType">
> >             <column name="Geometry" />
> >         </property>
> >
> > The property in the POJO ends up like this:
> >
> >      private Geometry geometry;
> >
> > which is a import com.vividsolutions.jts.geom.Geometry;
> >
> > However, when trying to execute an HQL or criteria query, I get the following:
> >
> > Exception in thread "main" java.lang.IllegalArgumentException: Expected byte array.
> >       at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48)
> >       at org.hibernatespatial.AbstractDBGeometryType.nullSafeGet(AbstractDBGeometryType.java:127)
> >       at org.hibernatespatial.GeometryUserType.nullSafeGet(GeometryUserType.java:172)
> >       at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:128)
> >       at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
> >       at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2114)
> >       at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
> >       at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
> >       at org.hibernate.loader.Loader.getRow(Loader.java:1230)
> >       at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
> >       at org.hibernate.loader.Loader.doQuery(Loader.java:724)
> >       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
> >       at org.hibernate.loader.Loader.doList(Loader.java:2232)
> >       at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
> >       at org.hibernate.loader.Loader.list(Loader.java:2124)
> >       at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
> >       at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
> >       at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
> >       at com.axiomalaska.TestClass.main(TestClass.java:36)
> >
> > Not sure what's going on here. I've tried changing the POJO geometry type to something more specific like com.vividsolutions.jts.geom.Point or com.vividsolutions.jts.geom.Polygon with no luck. I'm using JTDS, but I also tried using Microsoft's JDBC driver. Any ideas?
> >
> > Thank you,
> > Shane
> > _______________________________________________
> > hibernatespatial-users mailing list
> > [hidden email]
> > http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
>
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
>
>
>
> --
> Shane StClair
> Software Engineer
> Axiom Consulting & Design
> 523 W 8th Ave
> Suite 104
> Anchorage, AK 99501
> Website: http://www.axiomalaska.com
>
>
>
> --
> Shane StClair
> Software Engineer
> Axiom Consulting & Design
> 523 W 8th Ave
> Suite 104
> Anchorage, AK 99501
> Website: http://www.axiomalaska.com
>
>
>
> --
> Shane StClair
> Software Engineer
> Axiom Consulting & Design
> 523 W 8th Ave
> Suite 104
> Anchorage, AK 99501
> Website: http://www.axiomalaska.com
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Reply | Threaded
Open this post in threaded view
|

Re: SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

shane
Karel,

Over the weekend I successfully backported HS v1.1 to use Hibernate 3.3.2.GA. The main changes were the difference in the CustomType constructor that you mentioned and removing references to the new org.hibernate.type.StandardBasicTypes from the dialects (changed them back to use Hibernate.INTEGER, Hibernate.STRING, etc).

I also found a fix for the jTDS problem I was having when saving objects with geometry properties. I submitted a patch to them here:

I'm happy to report that with these changes Hibernate Spatial, jTDS, and Hibernate Tools are working together flawlessly. Hibernate Spatial is an awesome tool, many thanks!

There are a few changes I'd like to see make it into the HS source. One is the modification of SQLServerGeometryUserType's convert2JTS method that allows for Blobs in addition to byte[]s. My code with this change is in this thread. Is this sufficient, or would you prefer a patch against the trunk?

Also, I had to extend the SQL Server and Oracle dialects in a project to add dayofyear and simplify/reduce functions. To do this, I had to change the geometry CustomType variable in each HS provider dialect to be protected static final. Again, I can submit patches here if desired.

Thanks,
Shane

On Mon, Mar 21, 2011 at 4:52 AM, Karel Maesen <[hidden email]> wrote:
Hello Shane,

In the Hibernate 3.6 the CustomType construcor needs to passed a UserType instance where in earlier releases a UserType Class. So you're limited to test against version 1 (or 1.0.1) of Hibernate Spatial. You can check out the code from the tags directory. There are virtually no changes in the SQL Server support between version 1.0 and 1.1.

You could modify the 1.1 code to have
       CustomType geomType = new CustomType(SQLServerGeometryUserType.class, null );
to make it compatible with hibernate 3.3.2.GA.

Can you figure out how Hibernate Tools determines the type? If I find the time, I will also have a look at it.

Regards,

Karel

On 17 Mar 2011, at 01:04, Shane StClair wrote:

> Sorry to triple post, but I noticed that I didn't clearly communicate the CustomType error. When using null for the second argument (as shown in the last post), I get the error when reverse engineering in my app, which uses Hibernate 3.3.2.GA, so the problem is happening with that line when run against Hibernate 3.3.2.GA.
>
> On Wed, Mar 16, 2011 at 4:01 PM, Shane StClair <[hidden email]> wrote:
> Did some experimenting. The Microsoft JDBC 2.0 driver is able to read and update geometry data, but it isn't sufficient for reverse engineering. Reverse engineering via Hibernate Tools results in many fields being typed to Serializable, which breaks the existing app. I would have to specify the datatype of each field manually in the reverse engineering file, which mostly defeats the point of reverse engineering.
>
> jTDS still fails to update with the "Operand type clash: image is incompatible with geometry" error. I have a post asking for clarification on the jTDS help forum...
>
> I can't properly test Microsoft's 3.0 driver against the HS 1.1-SNAPSHOT because it uses Hibernate 3.6.0.Final and my app needs Hibernate 3.3.2.GA due to the use of Hibernate Tools (Hibernate Tools isn't compatible with 3.6.0). It's choking on line 58 of SqlServerSpatialDialect, where the new CustomType is created. I understand that this is due to a constructor change in Hibernate. It seems like I should be able to change this to the following:
>
> CustomType geomType = new CustomType(new SQLServerGeometryUserType(), null );
>
> but it's still giving me this error:
>
> java.lang.NoSuchMethodError: org.hibernate.type.CustomType.<init>(Lorg/hibernate/usertype/UserType;[Ljava/lang/String;)V
>         at org.hibernatespatial.sqlserver.SQLServerSpatialDialect.<init>(SQLServerSpatialDialect.java:59)
>         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
> ...etc
>
> Not sure why I'm getting this when using the MS 3.0 driver and not jTDS or MS 2.0.
>
> Unless you have any advice, it looks like I'm currently limited to the MS 2.0 driver, which won't work for this app. Thanks for the help.
> -Shane
>
> On Wed, Mar 16, 2011 at 1:05 PM, Shane StClair <[hidden email]> wrote:
> Hi Karel,
>
> Thanks very much for your quick response. I tried version 3.0 of Microsoft's driver. I believe that both jTDS and the 3.0 Microsoft driver return geometries as Blobs. If I update convert2JTS in SQLServerGeometryUserType to this, it works:
>
>     public Geometry convert2JTS(Object obj) {
>         byte[] raw = null;
>         if (obj == null)
>             return null;
>         if ( obj instanceof byte[] ) {
>             raw = (byte[]) obj;
>         } else if ( obj instanceof Blob ) {
>             Blob blob = (Blob) obj;
>             try {
>                 raw = blob.getBytes( 1, ( (Number) blob.length() ).intValue() );
>             } catch (SQLException e) {
>                 e.printStackTrace();
>             }
>         } else {
>             throw new IllegalArgumentException("Expected byte array or BLOB.");
>         }
>
>         return Decoders.decode(raw);
>     }
>
> With this change, I am able to read objects from the database and call methods on the Geometry java properties.
>
> However, when trying to save an object with a Geometry property I'm getting the following error:
>
> org.hibernate.exception.DataException: Could not execute JDBC batch update
>       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
>       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
>       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
>       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
>       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
>       at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
>       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
>       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
>       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
>       at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
>       at com.axiomalaska.adfg.aerialsurvey.util.HibernateUtil.commitTransaction(HibernateUtil.java:89)
>       at com.axiomalaska.adfg.aerialsurvey.spatial.EncodeDistrictPolygons.main(EncodeDistrictPolygons.java:30)
> Caused by: java.sql.BatchUpdateException: Operand type clash: image is incompatible with geometry
>       at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
>       at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
>       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
>       ... 9 more
>
> This is with the jTDS driver. I'm guessing that this is a problem with the driver and not with HS. I don't see geometry listed under jTDS' supported datatypes, so the driver may not know how to properly update geometry fields. I'll try with the Microsoft drivers and possibly ask on the jTDS mailing list.
>
> Thanks for your help.
> Shane
>
> On Tue, Mar 15, 2011 at 4:30 PM, Karel Maesen <[hidden email]> wrote:
> Hi Shane,
>
> When it's a Geometry or Geography column, the Microsoft JDBC driver is supposed to return a byte-array. In your case it clearly doesn't. Now I have tested HS against Microsoft JDBC driver version 2.0. What version of the MS driver did you use? I can't vouch for the JTDS driver, they may treat these types of objects differently. Could you check with them how they treat geometries?
>
> Could you try and figure out what is returned by the JDBC driver at the point the IllegalArgumentException is thrown?  Could you also post the HQL?
>
> Regards,
>
> Karel Maesen
>
>
> On 16 Mar 2011, at 01:06, Shane StClair wrote:
>
> > Hello,
> >
> > Hibernate Spatial looks very promising, however I'm having trouble getting it to work with SQL Server. I followed along with the Tutorial, making adjustments because I'm generating my hibernate mappings and POJOs via Hibernate Tools reverse engineering. I have tried several tables containing Geometry columns with simple data. The entry in the mapping looks like this:
> >
> >         <property name="geometry" type="org.hibernatespatial.GeometryUserType">
> >             <column name="Geometry" />
> >         </property>
> >
> > The property in the POJO ends up like this:
> >
> >      private Geometry geometry;
> >
> > which is a import com.vividsolutions.jts.geom.Geometry;
> >
> > However, when trying to execute an HQL or criteria query, I get the following:
> >
> > Exception in thread "main" java.lang.IllegalArgumentException: Expected byte array.
> >       at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48)
> >       at org.hibernatespatial.AbstractDBGeometryType.nullSafeGet(AbstractDBGeometryType.java:127)
> >       at org.hibernatespatial.GeometryUserType.nullSafeGet(GeometryUserType.java:172)
> >       at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:128)
> >       at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
> >       at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2114)
> >       at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
> >       at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
> >       at org.hibernate.loader.Loader.getRow(Loader.java:1230)
> >       at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
> >       at org.hibernate.loader.Loader.doQuery(Loader.java:724)
> >       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
> >       at org.hibernate.loader.Loader.doList(Loader.java:2232)
> >       at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
> >       at org.hibernate.loader.Loader.list(Loader.java:2124)
> >       at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
> >       at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
> >       at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
> >       at com.axiomalaska.TestClass.main(TestClass.java:36)
> >
> > Not sure what's going on here. I've tried changing the POJO geometry type to something more specific like com.vividsolutions.jts.geom.Point or com.vividsolutions.jts.geom.Polygon with no luck. I'm using JTDS, but I also tried using Microsoft's JDBC driver. Any ideas?
> >
> > Thank you,
> > Shane
> > _______________________________________________
> > hibernatespatial-users mailing list
> > [hidden email]
> > http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
>
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
>
>
>
> --
> Shane StClair
> Software Engineer
> Axiom Consulting & Design
> 523 W 8th Ave
> Suite 104
> Anchorage, AK 99501
> Website: http://www.axiomalaska.com
>
>
>
> --
> Shane StClair
> Software Engineer
> Axiom Consulting & Design
> 523 W 8th Ave
> Suite 104
> Anchorage, AK 99501
> Website: http://www.axiomalaska.com
>
>
>
> --
> Shane StClair
> Software Engineer
> Axiom Consulting & Design
> 523 W 8th Ave
> Suite 104
> Anchorage, AK 99501
> Website: http://www.axiomalaska.com
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users



--
Shane StClair
Software Engineer
Axiom Consulting & Design
523 W 8th Ave
Suite 104
Anchorage, AK 99501
Website: http://www.axiomalaska.com

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Reply | Threaded
Open this post in threaded view
|

Re: SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

Karel Maesen
Administrator
Hello Shane,

I seem to have forgotten to reply to this mail. Patches with your changes would be most welcome.

Regards,

Karel
On 21 Mar 2011, at 18:42, Shane StClair wrote:

> Karel,
>
> Over the weekend I successfully backported HS v1.1 to use Hibernate 3.3.2.GA. The main changes were the difference in the CustomType constructor that you mentioned and removing references to the new org.hibernate.type.StandardBasicTypes from the dialects (changed them back to use Hibernate.INTEGER, Hibernate.STRING, etc).
>
> I also found a fix for the jTDS problem I was having when saving objects with geometry properties. I submitted a patch to them here:
> http://sourceforge.net/tracker/?func=detail&aid=3224480&group_id=33291&atid=407764
>
> I'm happy to report that with these changes Hibernate Spatial, jTDS, and Hibernate Tools are working together flawlessly. Hibernate Spatial is an awesome tool, many thanks!
>
> There are a few changes I'd like to see make it into the HS source. One is the modification of SQLServerGeometryUserType's convert2JTS method that allows for Blobs in addition to byte[]s. My code with this change is in this thread. Is this sufficient, or would you prefer a patch against the trunk?
>
> Also, I had to extend the SQL Server and Oracle dialects in a project to add dayofyear and simplify/reduce functions. To do this, I had to change the geometry CustomType variable in each HS provider dialect to be protected static final. Again, I can submit patches here if desired.
>
> Thanks,
> Shane
>
> On Mon, Mar 21, 2011 at 4:52 AM, Karel Maesen <[hidden email]> wrote:
> Hello Shane,
>
> In the Hibernate 3.6 the CustomType construcor needs to passed a UserType instance where in earlier releases a UserType Class. So you're limited to test against version 1 (or 1.0.1) of Hibernate Spatial. You can check out the code from the tags directory. There are virtually no changes in the SQL Server support between version 1.0 and 1.1.
>
> You could modify the 1.1 code to have
>        CustomType geomType = new CustomType(SQLServerGeometryUserType.class, null );
> to make it compatible with hibernate 3.3.2.GA.
>
> Can you figure out how Hibernate Tools determines the type? If I find the time, I will also have a look at it.
>
> Regards,
>
> Karel
>
> On 17 Mar 2011, at 01:04, Shane StClair wrote:
>
> > Sorry to triple post, but I noticed that I didn't clearly communicate the CustomType error. When using null for the second argument (as shown in the last post), I get the error when reverse engineering in my app, which uses Hibernate 3.3.2.GA, so the problem is happening with that line when run against Hibernate 3.3.2.GA.
> >
> > On Wed, Mar 16, 2011 at 4:01 PM, Shane StClair <[hidden email]> wrote:
> > Did some experimenting. The Microsoft JDBC 2.0 driver is able to read and update geometry data, but it isn't sufficient for reverse engineering. Reverse engineering via Hibernate Tools results in many fields being typed to Serializable, which breaks the existing app. I would have to specify the datatype of each field manually in the reverse engineering file, which mostly defeats the point of reverse engineering.
> >
> > jTDS still fails to update with the "Operand type clash: image is incompatible with geometry" error. I have a post asking for clarification on the jTDS help forum...
> >
> > I can't properly test Microsoft's 3.0 driver against the HS 1.1-SNAPSHOT because it uses Hibernate 3.6.0.Final and my app needs Hibernate 3.3.2.GA due to the use of Hibernate Tools (Hibernate Tools isn't compatible with 3.6.0). It's choking on line 58 of SqlServerSpatialDialect, where the new CustomType is created. I understand that this is due to a constructor change in Hibernate. It seems like I should be able to change this to the following:
> >
> > CustomType geomType = new CustomType(new SQLServerGeometryUserType(), null );
> >
> > but it's still giving me this error:
> >
> > java.lang.NoSuchMethodError: org.hibernate.type.CustomType.<init>(Lorg/hibernate/usertype/UserType;[Ljava/lang/String;)V
> >         at org.hibernatespatial.sqlserver.SQLServerSpatialDialect.<init>(SQLServerSpatialDialect.java:59)
> >         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
> >         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
> > ...etc
> >
> > Not sure why I'm getting this when using the MS 3.0 driver and not jTDS or MS 2.0.
> >
> > Unless you have any advice, it looks like I'm currently limited to the MS 2.0 driver, which won't work for this app. Thanks for the help.
> > -Shane
> >
> > On Wed, Mar 16, 2011 at 1:05 PM, Shane StClair <[hidden email]> wrote:
> > Hi Karel,
> >
> > Thanks very much for your quick response. I tried version 3.0 of Microsoft's driver. I believe that both jTDS and the 3.0 Microsoft driver return geometries as Blobs. If I update convert2JTS in SQLServerGeometryUserType to this, it works:
> >
> >     public Geometry convert2JTS(Object obj) {
> >         byte[] raw = null;
> >         if (obj == null)
> >             return null;
> >         if ( obj instanceof byte[] ) {
> >             raw = (byte[]) obj;
> >         } else if ( obj instanceof Blob ) {
> >             Blob blob = (Blob) obj;
> >             try {
> >                 raw = blob.getBytes( 1, ( (Number) blob.length() ).intValue() );
> >             } catch (SQLException e) {
> >                 e.printStackTrace();
> >             }
> >         } else {
> >             throw new IllegalArgumentException("Expected byte array or BLOB.");
> >         }
> >
> >         return Decoders.decode(raw);
> >     }
> >
> > With this change, I am able to read objects from the database and call methods on the Geometry java properties.
> >
> > However, when trying to save an object with a Geometry property I'm getting the following error:
> >
> > org.hibernate.exception.DataException: Could not execute JDBC batch update
> >       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
> >       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
> >       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
> >       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
> >       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
> >       at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
> >       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
> >       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
> >       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
> >       at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
> >       at com.axiomalaska.adfg.aerialsurvey.util.HibernateUtil.commitTransaction(HibernateUtil.java:89)
> >       at com.axiomalaska.adfg.aerialsurvey.spatial.EncodeDistrictPolygons.main(EncodeDistrictPolygons.java:30)
> > Caused by: java.sql.BatchUpdateException: Operand type clash: image is incompatible with geometry
> >       at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
> >       at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
> >       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
> >       ... 9 more
> >
> > This is with the jTDS driver. I'm guessing that this is a problem with the driver and not with HS. I don't see geometry listed under jTDS' supported datatypes, so the driver may not know how to properly update geometry fields. I'll try with the Microsoft drivers and possibly ask on the jTDS mailing list.
> >
> > Thanks for your help.
> > Shane
> >
> > On Tue, Mar 15, 2011 at 4:30 PM, Karel Maesen <[hidden email]> wrote:
> > Hi Shane,
> >
> > When it's a Geometry or Geography column, the Microsoft JDBC driver is supposed to return a byte-array. In your case it clearly doesn't. Now I have tested HS against Microsoft JDBC driver version 2.0. What version of the MS driver did you use? I can't vouch for the JTDS driver, they may treat these types of objects differently. Could you check with them how they treat geometries?
> >
> > Could you try and figure out what is returned by the JDBC driver at the point the IllegalArgumentException is thrown?  Could you also post the HQL?
> >
> > Regards,
> >
> > Karel Maesen
> >
> >
> > On 16 Mar 2011, at 01:06, Shane StClair wrote:
> >
> > > Hello,
> > >
> > > Hibernate Spatial looks very promising, however I'm having trouble getting it to work with SQL Server. I followed along with the Tutorial, making adjustments because I'm generating my hibernate mappings and POJOs via Hibernate Tools reverse engineering. I have tried several tables containing Geometry columns with simple data. The entry in the mapping looks like this:
> > >
> > >         <property name="geometry" type="org.hibernatespatial.GeometryUserType">
> > >             <column name="Geometry" />
> > >         </property>
> > >
> > > The property in the POJO ends up like this:
> > >
> > >      private Geometry geometry;
> > >
> > > which is a import com.vividsolutions.jts.geom.Geometry;
> > >
> > > However, when trying to execute an HQL or criteria query, I get the following:
> > >
> > > Exception in thread "main" java.lang.IllegalArgumentException: Expected byte array.
> > >       at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48)
> > >       at org.hibernatespatial.AbstractDBGeometryType.nullSafeGet(AbstractDBGeometryType.java:127)
> > >       at org.hibernatespatial.GeometryUserType.nullSafeGet(GeometryUserType.java:172)
> > >       at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:128)
> > >       at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
> > >       at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2114)
> > >       at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
> > >       at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
> > >       at org.hibernate.loader.Loader.getRow(Loader.java:1230)
> > >       at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
> > >       at org.hibernate.loader.Loader.doQuery(Loader.java:724)
> > >       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
> > >       at org.hibernate.loader.Loader.doList(Loader.java:2232)
> > >       at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
> > >       at org.hibernate.loader.Loader.list(Loader.java:2124)
> > >       at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
> > >       at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
> > >       at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
> > >       at com.axiomalaska.TestClass.main(TestClass.java:36)
> > >
> > > Not sure what's going on here. I've tried changing the POJO geometry type to something more specific like com.vividsolutions.jts.geom.Point or com.vividsolutions.jts.geom.Polygon with no luck. I'm using JTDS, but I also tried using Microsoft's JDBC driver. Any ideas?
> > >
> > > Thank you,
> > > Shane
> > > _______________________________________________
> > > hibernatespatial-users mailing list
> > > [hidden email]
> > > http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
> >
> > _______________________________________________
> > hibernatespatial-users mailing list
> > [hidden email]
> > http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
> >
> >
> >
> > --
> > Shane StClair
> > Software Engineer
> > Axiom Consulting & Design
> > 523 W 8th Ave
> > Suite 104
> > Anchorage, AK 99501
> > Website: http://www.axiomalaska.com
> >
> >
> >
> > --
> > Shane StClair
> > Software Engineer
> > Axiom Consulting & Design
> > 523 W 8th Ave
> > Suite 104
> > Anchorage, AK 99501
> > Website: http://www.axiomalaska.com
> >
> >
> >
> > --
> > Shane StClair
> > Software Engineer
> > Axiom Consulting & Design
> > 523 W 8th Ave
> > Suite 104
> > Anchorage, AK 99501
> > Website: http://www.axiomalaska.com
> > _______________________________________________
> > hibernatespatial-users mailing list
> > [hidden email]
> > http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
>
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
>
>
>
> --
> Shane StClair
> Software Engineer
> Axiom Consulting & Design
> 523 W 8th Ave
> Suite 104
> Anchorage, AK 99501
> Website: http://www.axiomalaska.com
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Reply | Threaded
Open this post in threaded view
|

Re: SQLServerSpatialDialect problems ("Expected byte array" at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48) )

shane
Done and done :)

http://www.hibernatespatial.org/jira/browse/HIBSPA-81

On Wed, Mar 30, 2011 at 5:09 AM, Karel Maesen <[hidden email]> wrote:
Hello Shane,

I seem to have forgotten to reply to this mail. Patches with your changes would be most welcome.

Regards,

Karel
On 21 Mar 2011, at 18:42, Shane StClair wrote:

> Karel,
>
> Over the weekend I successfully backported HS v1.1 to use Hibernate 3.3.2.GA. The main changes were the difference in the CustomType constructor that you mentioned and removing references to the new org.hibernate.type.StandardBasicTypes from the dialects (changed them back to use Hibernate.INTEGER, Hibernate.STRING, etc).
>
> I also found a fix for the jTDS problem I was having when saving objects with geometry properties. I submitted a patch to them here:
> http://sourceforge.net/tracker/?func=detail&aid=3224480&group_id=33291&atid=407764
>
> I'm happy to report that with these changes Hibernate Spatial, jTDS, and Hibernate Tools are working together flawlessly. Hibernate Spatial is an awesome tool, many thanks!
>
> There are a few changes I'd like to see make it into the HS source. One is the modification of SQLServerGeometryUserType's convert2JTS method that allows for Blobs in addition to byte[]s. My code with this change is in this thread. Is this sufficient, or would you prefer a patch against the trunk?
>
> Also, I had to extend the SQL Server and Oracle dialects in a project to add dayofyear and simplify/reduce functions. To do this, I had to change the geometry CustomType variable in each HS provider dialect to be protected static final. Again, I can submit patches here if desired.
>
> Thanks,
> Shane
>
> On Mon, Mar 21, 2011 at 4:52 AM, Karel Maesen <[hidden email]> wrote:
> Hello Shane,
>
> In the Hibernate 3.6 the CustomType construcor needs to passed a UserType instance where in earlier releases a UserType Class. So you're limited to test against version 1 (or 1.0.1) of Hibernate Spatial. You can check out the code from the tags directory. There are virtually no changes in the SQL Server support between version 1.0 and 1.1.
>
> You could modify the 1.1 code to have
>        CustomType geomType = new CustomType(SQLServerGeometryUserType.class, null );
> to make it compatible with hibernate 3.3.2.GA.
>
> Can you figure out how Hibernate Tools determines the type? If I find the time, I will also have a look at it.
>
> Regards,
>
> Karel
>
> On 17 Mar 2011, at 01:04, Shane StClair wrote:
>
> > Sorry to triple post, but I noticed that I didn't clearly communicate the CustomType error. When using null for the second argument (as shown in the last post), I get the error when reverse engineering in my app, which uses Hibernate 3.3.2.GA, so the problem is happening with that line when run against Hibernate 3.3.2.GA.
> >
> > On Wed, Mar 16, 2011 at 4:01 PM, Shane StClair <[hidden email]> wrote:
> > Did some experimenting. The Microsoft JDBC 2.0 driver is able to read and update geometry data, but it isn't sufficient for reverse engineering. Reverse engineering via Hibernate Tools results in many fields being typed to Serializable, which breaks the existing app. I would have to specify the datatype of each field manually in the reverse engineering file, which mostly defeats the point of reverse engineering.
> >
> > jTDS still fails to update with the "Operand type clash: image is incompatible with geometry" error. I have a post asking for clarification on the jTDS help forum...
> >
> > I can't properly test Microsoft's 3.0 driver against the HS 1.1-SNAPSHOT because it uses Hibernate 3.6.0.Final and my app needs Hibernate 3.3.2.GA due to the use of Hibernate Tools (Hibernate Tools isn't compatible with 3.6.0). It's choking on line 58 of SqlServerSpatialDialect, where the new CustomType is created. I understand that this is due to a constructor change in Hibernate. It seems like I should be able to change this to the following:
> >
> > CustomType geomType = new CustomType(new SQLServerGeometryUserType(), null );
> >
> > but it's still giving me this error:
> >
> > java.lang.NoSuchMethodError: org.hibernate.type.CustomType.<init>(Lorg/hibernate/usertype/UserType;[Ljava/lang/String;)V
> >         at org.hibernatespatial.sqlserver.SQLServerSpatialDialect.<init>(SQLServerSpatialDialect.java:59)
> >         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
> >         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
> > ...etc
> >
> > Not sure why I'm getting this when using the MS 3.0 driver and not jTDS or MS 2.0.
> >
> > Unless you have any advice, it looks like I'm currently limited to the MS 2.0 driver, which won't work for this app. Thanks for the help.
> > -Shane
> >
> > On Wed, Mar 16, 2011 at 1:05 PM, Shane StClair <[hidden email]> wrote:
> > Hi Karel,
> >
> > Thanks very much for your quick response. I tried version 3.0 of Microsoft's driver. I believe that both jTDS and the 3.0 Microsoft driver return geometries as Blobs. If I update convert2JTS in SQLServerGeometryUserType to this, it works:
> >
> >     public Geometry convert2JTS(Object obj) {
> >         byte[] raw = null;
> >         if (obj == null)
> >             return null;
> >         if ( obj instanceof byte[] ) {
> >             raw = (byte[]) obj;
> >         } else if ( obj instanceof Blob ) {
> >             Blob blob = (Blob) obj;
> >             try {
> >                 raw = blob.getBytes( 1, ( (Number) blob.length() ).intValue() );
> >             } catch (SQLException e) {
> >                 e.printStackTrace();
> >             }
> >         } else {
> >             throw new IllegalArgumentException("Expected byte array or BLOB.");
> >         }
> >
> >         return Decoders.decode(raw);
> >     }
> >
> > With this change, I am able to read objects from the database and call methods on the Geometry java properties.
> >
> > However, when trying to save an object with a Geometry property I'm getting the following error:
> >
> > org.hibernate.exception.DataException: Could not execute JDBC batch update
> >       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
> >       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
> >       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
> >       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
> >       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
> >       at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
> >       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
> >       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
> >       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
> >       at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
> >       at com.axiomalaska.adfg.aerialsurvey.util.HibernateUtil.commitTransaction(HibernateUtil.java:89)
> >       at com.axiomalaska.adfg.aerialsurvey.spatial.EncodeDistrictPolygons.main(EncodeDistrictPolygons.java:30)
> > Caused by: java.sql.BatchUpdateException: Operand type clash: image is incompatible with geometry
> >       at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:947)
> >       at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
> >       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
> >       ... 9 more
> >
> > This is with the jTDS driver. I'm guessing that this is a problem with the driver and not with HS. I don't see geometry listed under jTDS' supported datatypes, so the driver may not know how to properly update geometry fields. I'll try with the Microsoft drivers and possibly ask on the jTDS mailing list.
> >
> > Thanks for your help.
> > Shane
> >
> > On Tue, Mar 15, 2011 at 4:30 PM, Karel Maesen <[hidden email]> wrote:
> > Hi Shane,
> >
> > When it's a Geometry or Geography column, the Microsoft JDBC driver is supposed to return a byte-array. In your case it clearly doesn't. Now I have tested HS against Microsoft JDBC driver version 2.0. What version of the MS driver did you use? I can't vouch for the JTDS driver, they may treat these types of objects differently. Could you check with them how they treat geometries?
> >
> > Could you try and figure out what is returned by the JDBC driver at the point the IllegalArgumentException is thrown?  Could you also post the HQL?
> >
> > Regards,
> >
> > Karel Maesen
> >
> >
> > On 16 Mar 2011, at 01:06, Shane StClair wrote:
> >
> > > Hello,
> > >
> > > Hibernate Spatial looks very promising, however I'm having trouble getting it to work with SQL Server. I followed along with the Tutorial, making adjustments because I'm generating my hibernate mappings and POJOs via Hibernate Tools reverse engineering. I have tried several tables containing Geometry columns with simple data. The entry in the mapping looks like this:
> > >
> > >         <property name="geometry" type="org.hibernatespatial.GeometryUserType">
> > >             <column name="Geometry" />
> > >         </property>
> > >
> > > The property in the POJO ends up like this:
> > >
> > >      private Geometry geometry;
> > >
> > > which is a import com.vividsolutions.jts.geom.Geometry;
> > >
> > > However, when trying to execute an HQL or criteria query, I get the following:
> > >
> > > Exception in thread "main" java.lang.IllegalArgumentException: Expected byte array.
> > >       at org.hibernatespatial.sqlserver.SQLServerGeometryUserType.convert2JTS(SQLServerGeometryUserType.java:48)
> > >       at org.hibernatespatial.AbstractDBGeometryType.nullSafeGet(AbstractDBGeometryType.java:127)
> > >       at org.hibernatespatial.GeometryUserType.nullSafeGet(GeometryUserType.java:172)
> > >       at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:128)
> > >       at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
> > >       at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2114)
> > >       at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
> > >       at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
> > >       at org.hibernate.loader.Loader.getRow(Loader.java:1230)
> > >       at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
> > >       at org.hibernate.loader.Loader.doQuery(Loader.java:724)
> > >       at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
> > >       at org.hibernate.loader.Loader.doList(Loader.java:2232)
> > >       at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
> > >       at org.hibernate.loader.Loader.list(Loader.java:2124)
> > >       at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
> > >       at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
> > >       at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
> > >       at com.axiomalaska.TestClass.main(TestClass.java:36)
> > >
> > > Not sure what's going on here. I've tried changing the POJO geometry type to something more specific like com.vividsolutions.jts.geom.Point or com.vividsolutions.jts.geom.Polygon with no luck. I'm using JTDS, but I also tried using Microsoft's JDBC driver. Any ideas?
> > >
> > > Thank you,
> > > Shane
> > > _______________________________________________
> > > hibernatespatial-users mailing list
> > > [hidden email]
> > > http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
> >
> > _______________________________________________
> > hibernatespatial-users mailing list
> > [hidden email]
> > http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
> >
> >
> >
> > --
> > Shane StClair
> > Software Engineer
> > Axiom Consulting & Design
> > 523 W 8th Ave
> > Suite 104
> > Anchorage, AK 99501
> > Website: http://www.axiomalaska.com
> >
> >
> >
> > --
> > Shane StClair
> > Software Engineer
> > Axiom Consulting & Design
> > 523 W 8th Ave
> > Suite 104
> > Anchorage, AK 99501
> > Website: http://www.axiomalaska.com
> >
> >
> >
> > --
> > Shane StClair
> > Software Engineer
> > Axiom Consulting & Design
> > 523 W 8th Ave
> > Suite 104
> > Anchorage, AK 99501
> > Website: http://www.axiomalaska.com
> > _______________________________________________
> > hibernatespatial-users mailing list
> > [hidden email]
> > http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
>
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
>
>
>
> --
> Shane StClair
> Software Engineer
> Axiom Consulting & Design
> 523 W 8th Ave
> Suite 104
> Anchorage, AK 99501
> Website: http://www.axiomalaska.com
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users



--
Shane StClair
Software Engineer
Axiom Consulting & Design
523 W 8th Ave
Suite 104
Anchorage, AK 99501
http://www.axiomalaska.com

_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users