【发布时间】:2019-04-08 08:30:46
【问题描述】:
我正在尝试向 Grails 3.3.8(当前最新版本)中的域对象添加一个点。 Grails 3.3.8 使用 Hibernate 5.1.5,它支持 hibernate-spatial。
在 build.gradle 中:
compile group: 'org.hibernate', name: 'hibernate-spatial', version: '5.1.5.Final'
compile group: 'com.vividsolutions', name: 'jts', version: '1.13'
在配置中:
driverClassName = 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
dialect = 'org.hibernate.dialect.SqlServer2008SpatialDialect'
在域中,PointTest.groovy:
package com.test
import com.vividsolutions.jts.geom.Point
class PointTest {
Point coords
static constraints = {
}
static mapping = {
coords sqlType: 'geometry(Point,4326)'
}
}
据我从this post on Stack Overflow 得知,以上应该可行。但是当我运行 Grails 项目时,没有创建表。如果我从mapping 中删除该行,则会创建表,但coords 的类型错误,varbinary(255)。
Java 版本:8
Grails 版本:3.3.8
数据库版本:SQL Server 2017
【问题讨论】:
-
“但是当我运行 Grails 项目时,没有创建表。” - 没有创建哪个表?
-
我现在没有设置在这里使用 SQL Server 进行测试,这可能是相关的,但是当我在带有 H2 的 3.3.8 应用程序中测试您的代码时,我看到正在生成以下 DDL:
create table point_test (id bigint generated by default as identity, version bigint not null, coords geometry(Point,4326) not null, primary key (id)).如果您使用 H2,您会看到同样的结果吗? -
那个
geometry(Point,4326)不会被数据库识别。 -
point_test 表根本没有创建。整个表的创建被跳过。我会尝试使用 H2 并回复您。不过,我确实希望该列设置为 SRID 4326。
-
在对 H2 进行双重检查的过程中,我意识到我的 Sql Server 方言路径不正确。正确的路径是 org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect。更正该路径后,我得到以下信息:创建表 point_test(id bigint identity not null,version bigint not null,coords GEOMETRY not null,主键(id))。我不确定如何让它使用 SRID 4326,但我可能需要在初始化时为每个对象执行此操作。
标签: grails grails-orm hibernate-spatial grails-3.3.x