MSSql服务器jpa空间异常

时间:2017-07-13 14:09:25

标签: java sql hibernate jpa spatial

是否可以在jpa中使用sql空间数据?我有MS SQL Server 2014 Express Edition。我试图使用空间数据如下;

  • maven(pom.xml)依赖项;

    <dependency>
        <groupId>com.vividsolutions</groupId>
        <artifactId>jts</artifactId>
        <version>1.13</version>
    </dependency>
    
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    
  • db dialect;

spring.jpa.hibernate.dialect = org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect

  • 实体定义;

    @Column(columnDefinition =“Geometry”)
    私人Point位置;

  • 创建数据;

    SampleEntity se = new SampleEntity();
    se.setName( “样本”);
    se.setAge(30);
    GeometryFactory gf = new GeometryFactory(new PrecisionModel(),4326);
    Point location1 = gf.createPoint(new Coordinate(0,0,4384));
    location1.setSRID(4326);
    se.setLocation(LOCATION1);
    System.out.println(“LOCATION 1 SRID:”+ se.getLocation()。getSRID());
    System.out.println(“geometry srid:”+ gf.getSRID());
    sampleEntityRepository.save(SE);

我得到以下异常;

“Hibernate:插入sample_entity(age,author,date_created,date_updated,location,name)值(?,?,?,?,?,?)

2017-07-13 16:49:53.655 WARN 6600 --- [main] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:6522,SQLState:S0001

2017-07-13 16:49:53.656 ERROR 6600 --- [main] ohengine.jdbc.spi.SqlExceptionHelper:在执行用户定义的例程或聚合“geography”期间发生.NET Framework错误:< / p>

System.ArgumentException:24204:空间参考标识符(SRID)无效。指定的SRID必须与sys.spatial_reference_systems目录视图中显示的受支持的SRID之一匹配。

System.ArgumentException:

at Microsoft.SqlServer.Types.SqlGeography.set_Srid(Int32 value)

在Microsoft.SqlServer.Types.SqlGeography.Read(BinaryReader r)

在SqlGeography ::。DeserializeValidate(IntPtr,Int32,CClrLobContext *) 。“

有什么解决方案吗?或者,我做错了吗?

1 个答案:

答案 0 :(得分:1)

您遇到的问题是由于指定了不正确的方言属性:

<块引用>

spring.jpa.hibernate.dialect

应该

<块引用>

spring.jpa.properties.hibernate.dialect

整个字符串是:“spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect”

然后,一切都应该正常工作。可以在spring的日志中看到:

org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect