如何将地图几何对象存储到 Java 中的 MS-SQL 几何列?

How to store map geometry object to MS-SQL geometry column in Java?

我正在尝试将几何对象存储到我的 MS-SQL 数据库中,该数据库有一个 table 和一个几何列。我得到 JSON 格式的几何图形。

Here 我得到了最新的 MSSQL-JDBC 版本,其数据类型为 'com.microsoft.sqlserver.jdbc.Geometry'.
在 Maven pom.xml.

中包含所需的依赖项后,此数据类型可用

但是当我在 java 实体 [=51= 中提到我的一个 MS-SQL 几何列数据类型为 'com.microsoft.sqlserver.jdbc.Geometry' ] 和 运行 应用程序,它抛出如下错误:

> Exception encountered during context initialization -  cancelling refresh attempt:  
org.springframework.beans.factory.BeanCreationException:  
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:  
Invocation of init method failed; nested exception is  javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is  org.hibernate.MappingException: Could not determine type for: com.microsoft.sqlserver.jdbc.Geometry, at table: GeoTable, for columns:  
[org.hibernate.mapping.Column(request_point)]

下面是代码示例,

Entity class
import com.microsoft.sqlserver.jdbc.Geometry;

@Column(name = "request_point", columnDefinition = "Geometry")
private Geometry request_point;

pom.xml:

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
     <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>7.0.0.jre10</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.10.Final</version>
    </dependency>

我的 application.properties

中有以下几行
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true

示例几何字符串 -

{\"spatialReference\": {\"latestWkid\": 3434,\"wkid\": 4353}, \"x\": -10538019.079024673,\"y\": 4720603.9173474545}

我不明白为什么我的几何数据类型没有被加载,如果我遗漏了任何东西或任何其他方法来做同样的事情,请告诉我。

任何帮助将不胜感激。

我猜原因是 Hibernate 对类型一无所知 com.microsoft.sqlserver.jdbc.Geometry

我注意到您已经将 hibernate-spatial 作为依赖项包含在内。 Hibernate Spatial 提供了独立于数据库的几何类型。参见 documentation

谢谢 Karel,现在我可以使用 JpaRepository 将几何对象保存到数据库中。

完成的代码更改如下所述,

@Entity class
import com.vividsolutions.jts.geom.Geometry;

@JsonSerialize(using = GeometryToJsonSerializer.class)
@JsonDeserialize(using = JsonToGeometryDeserializer.class)
@Column(name = "request_point", columnDefinition = "Geometry")
private Geometry request_point;


// JsonDeserializer method 
public class JsonToGeometryDeserializer extends JsonDeserializer<Geometry> {
@Override
public Geometry deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    try {
        String text = jp.getText();
        if (text == null || text.length() <= 0)
            return null;

        MapGeometry geo = GeometryEngine.jsonToGeometry(text);
        String geomWkt = GeometryEngine.geometryToWkt(geo.getGeometry(), 0);

          WKTReader wktR = new WKTReader();
          Geometry geom = wktR.read(geomWkt);
          geom.setSRID(geo.getSpatialReference().getID());
        return geom;

    } catch (Exception e) {
        return null;

    }
}

}

通过使用这个 JsonDeserializer,我可以将 json 几何图形转换为几何对象并成功保存到 MsSQL 数据库。

现在我无法从 MsSQL 数据库中检索几何对象。

我尝试使用下面的示例将几何列作为字符串获取,但出现错误,

@Query(value = "select request_point.STAsText() request_point from tablename where locate_request_guid=1?", nativeQuery = true)
String findByGUID(String guid);

但同样适用于 jdbcTemplate

  @Transactional(readOnly=true)
public locate_requestJDBC findUserById(String GUID) {
    return jdbcTemplate.queryForObject(
        "select locate_request_guid,user_name,work_description,request_point.STAsText() request_point, request_polygon.STAsText() request_polygon from tablename where locate_request_guid=?",
        new Object[]{GUID}, new LocateRowMapper());
}

正确的做法是什么,任何输入都会有所帮助。