System.ArgumentException:24100:空间参考标识符(SRID)无效。 SRID必须介于0到999999之间

时间:2019-10-02 09:14:45

标签: sql-server mssql-jdbc hibernate-spatial

我想在数据库中保存新的几何数据,但是我一直都在收到此错误消息。

  

.NET Framework错误在执行用户定义的例程或聚合“几何”期间发生。   System.ArgumentException:24100:空间参考标识符(SRID)无效。 SRID必须在0到999999之间。

记录

2019-10-02 06:00:41.009 DEBUG 55688 --- [on(2)-127.0.0.1] o.h.e.j.e.i.JdbcEnvironmentInitiator     : Database ->
       name : Microsoft SQL Server
    version : 14.00.1000
      major : 14
      minor : 0
2019-10-02 06:00:41.010 DEBUG 55688 --- [on(2)-127.0.0.1] o.h.e.j.e.i.JdbcEnvironmentInitiator     : Driver ->
       name : Microsoft JDBC Driver 7.4 for SQL Server
    version : 7.4.1.0
      major : 7
      minor : 4
2019-10-02 06:00:41.010 DEBUG 55688 --- [on(2)-127.0.0.1] o.h.e.j.e.i.JdbcEnvironmentInitiator     : JDBC version : 4.2
2019-10-02 06:00:41.054  INFO 55688 --- [on(2)-127.0.0.1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect

...

2019-10-02 05:50:22.232 DEBUG 62340 --- [nio-8080-exec-6] org.hibernate.SQL                        : insert into teste_geo (geom, nome) values (?, ?)
Hibernate: insert into teste_geo (geom, nome) values (?, ?)
2019-10-02 05:50:22.232 TRACE 62340 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARBINARY] - [POLYGON ((4 0, 2 2, 4 4, 6 2, 4 0))]
2019-10-02 05:50:22.232 TRACE 62340 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [5f230d1b-ad0d-44a8-997e-02f4533bcfcd]
2019-10-02 05:50:26.452  INFO 62340 --- [   scheduling-1] c.v.g.o.service.ExemploService           : Executou chamada do servico!
2019-10-02 05:50:26.452  WARN 62340 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 6522, SQLState: S0001

我的班级代码

import lombok.Getter;
import lombok.Setter;
import org.locationtech.jts.geom.Geometry;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "TESTE_GEO")
@Getter
@Setter
public class TesteGeom {
    @Id
    @Column(name = "nome")
    private String name;
    @Column(name = "geom")
    private Geometry geometry;
}

...........

        UUID idUnique = UUID.randomUUID();
        TesteGeom t = new TesteGeom();
        t.setName(idUnique.toString());

        GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel() ,4326); 

        Coordinate[] coords  =
                new Coordinate[] {new Coordinate(4, 0), new Coordinate(2, 2),
                        new Coordinate(4, 4), new Coordinate(6, 2), new Coordinate(4, 0) };

        LinearRing ring = geometryFactory.createLinearRing( coords );
        LinearRing holes[] = null; // use LinearRing[] to represent holes
        int SRID =  geometryFactory.getSRID();
        Polygon polygon = geometryFactory.createPolygon(ring, holes );

       t.setGeometry(polygon);

       t.getGeometry().setSRID(4326);

在Management Studio上执行相同的SQL即可!

insert into teste_geo (geom, nome) values ('POLYGON ((4 0, 2 2, 4 4, 6 2, 4 0))', 'OK');

nome    varchar(50) 
geom    geometry    

在查询中,我得到了错误。可能有一些方言错误。

org.springframework.orm.jpa.JpaSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:351)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)

1 个答案:

答案 0 :(得分:0)

我找到了解决方法。

在我的模型上,我正在使用:

import org.locationtech.jts.geom.Geometry;

但是Hibernate Spatial只接受:

import com.vividsolutions.jts.geom.Geometry;

因此,我更改了lib,一切正常。