Hibernate persist failure with PostGIS Geometry

前端 未结 3 1726
长情又很酷
长情又很酷 2020-12-31 19:07

Related to previous question. I have a Spring Roo application using Hibernate to write a Geometry object to a PostGIS database using JTS. I believe I\'ve fixed the problems

相关标签:
3条回答
  • 2020-12-31 19:41

    Yes, the ? are substituted by the values you need to store.

    Did you try to use the following type: GeometryUserType and not the GeometryType? I suspect GeometryType is not directly supported by the API of Hibernate Spatial Project. It is maybe an abstract class which you could not instantiate directly to map your datas with annotations - it acts beyond the scene as we have experimented.

    Caused by: java.lang.UnsupportedOperationException which has make me tell that.

    And the last XML stuff inside the tutorial you have followed is clear:

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

    Looking at the code inside the GeometryUserType I see only one place where these exception could be thrown.

    public Object conv2DBGeometry(Geometry jtsGeom, Connection connection) {
            org.postgis.Geometry geom = null;
            jtsGeom = forceEmptyToGeometryCollection(jtsGeom);
            if (jtsGeom instanceof com.vividsolutions.jts.geom.Point) {
                geom = convertJTSPoint((com.vividsolutions.jts.geom.Point) jtsGeom);
            } else if (jtsGeom instanceof com.vividsolutions.jts.geom.LineString) {
                geom = convertJTSLineString((com.vividsolutions.jts.geom.LineString) jtsGeom);
            } else if (jtsGeom instanceof com.vividsolutions.jts.geom.MultiLineString) {
                geom = convertJTSMultiLineString((com.vividsolutions.jts.geom.MultiLineString) jtsGeom);
            } else if (jtsGeom instanceof com.vividsolutions.jts.geom.Polygon) {
                geom = convertJTSPolygon((com.vividsolutions.jts.geom.Polygon) jtsGeom);
            } else if (jtsGeom instanceof com.vividsolutions.jts.geom.MultiPoint) {
                geom = convertJTSMultiPoint((com.vividsolutions.jts.geom.MultiPoint) jtsGeom);
            } else if (jtsGeom instanceof com.vividsolutions.jts.geom.MultiPolygon) {
                geom = convertJTSMultiPolygon((com.vividsolutions.jts.geom.MultiPolygon) jtsGeom);
            } else if (jtsGeom instanceof com.vividsolutions.jts.geom.GeometryCollection) {
                geom = convertJTSGeometryCollection((com.vividsolutions.jts.geom.GeometryCollection) jtsGeom);
            }
    
            if (geom != null)
                return new PGgeometry(geom);
            else
                throw new UnsupportedOperationException("Conversion of "
                        + jtsGeom.getClass().getSimpleName()
                        + " to PGgeometry not supported");
        }
    

    Where PGgeometry stands for PostGis Geometry I think (or maybe PostgreSQL).

    I have found some topics where Karel Maesen and others speak about the InnoDB support is not very well, but they are maybe outdated (05-2011).

    Good luck!

    0 讨论(0)
  • 2020-12-31 19:46

    I got this exception when I forgot to add the Postgis Dialect in hibernate configuration file.

    Add following line to hibernate.cfg.xml

    <property name="dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect</property>
    
    0 讨论(0)
  • 2020-12-31 19:47

    It seems the problem was that the PostgisDialect was not been picked up and integrated correctly, and hence the required operations were not supported. The solution was as simple as upgrading from Hibernate 3.6.9.Final to 4.1.6.Final!

    See my thread on the mailing list for more information.

    As per that thread, you should also be aware that as of Hibernate Spatial 4.0-M1, only the Geometry type is specified to Hibernate, and hence the @Column annotation must set columnDefinition="Geometry", and not Point or anything else. This may be fixed in the future.

    With this anthology of modifications, I can finally write a Point to a database! The correct property specification is:

    @Column(columnDefinition="Geometry")
    @Type(type = "org.hibernate.spatial.GeometryType")
    private Point centerPoint;
    
    0 讨论(0)
提交回复
热议问题