我正在尝试将一个几何对象存储到我的MS-SQL数据库中,该数据库有一个包含几何列的表。我得到JSON格式的几何图形。
我得到了数据类型为'com.microsoft.sqlserver.jdbc.Geometry'
.的Here -JDBC的最新版本
在maven pom.xml
中包含了所需的依赖项之后,就可以使用此数据类型。
但是,当我在java实体类中提到我的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}
我不明白为什么我的几何数据类型没有被加载,如果我遗漏了什么或者任何其他方法来做同样的事情,请告诉我。
任何帮助都将不胜感激。
发布于 2018-09-20 15:29:36
感谢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());
}
做这件事的正确方法是什么,任何输入都会有帮助。
发布于 2018-09-15 03:12:53
我猜原因是Hibernate对com.microsoft.sqlserver.jdbc.Geometry
类型一无所知。
我注意到您已经包含了hibernate-spatial作为依赖项。Hibernate Spatial提供了独立于数据库的Geometry类型。请参阅documentation
https://stackoverflow.com/questions/52296537
复制相似问题