【问题标题】:How to store map geometry object to MS-SQL geometry column in Java?如何将地图几何对象存储到 Java 中的 MS-SQL 几何列?
【发布时间】:2019-02-17 04:12:27
【问题描述】:

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

Here 我得到了最新的 MSSQL-JDBC 版本,它的数据类型为 'com.microsoft.sqlserver.jdbc.Geometry'
在 maven pom.xml 中包含所需的依赖项后,此数据类型可用。

但是当我在 java Entity 类中提到我的 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}

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

任何帮助将不胜感激。

【问题讨论】:

    标签: java spring-boot mssql-jdbc hibernate-spatial


    【解决方案1】:

    感谢 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());
    }
    

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 多谢回复,那么必须使用哪种几何类型呢?我尝试使用 com.vividsolutions.jts.geom.Geometry 和 com.esri.core.geometry.Geometry 但 MSSQL 不支持这些。
      • 支持 com.vividsolutions.jts.geom.Geometry。 MSSQL 支持什么并不重要。 Hibernate Spatial 处理 JTS Geometry 和数据库之间的转换。但是,您将需要使用空间方言。在您的情况下,应该是 org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
      • 感谢您提供详细信息,您是否有任何将 Geometry Json 转换为 com.vividsolutions.jts.geom.Geometry 对象的示例链接。
      • 您可以在这里查看:github.com/GeoLatte/geolatte-geom。 Geolatte 有一个基于 Jackson 的 GeoJson ObjectMapper。您可以从 Geolatte-geom 转换为 JTS(请参阅 Geolatte JTS 类)。还有一个使用 Spring JPA 和 Geolatte/GeoJson 的教程(正在进行中):github.com/maesenka/hibernate-spatial-tutorials/tree/master/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多