Nik*_*ikS 2 java hibernate h2 spring-boot
我正在尝试在 Spring Boot 应用程序中设置一个带有 H2GIS 扩展的内存中 H2 数据库。下面提供了我的build.gradle和application.properties。根据H2GIS 文档和大量示例,扩展必须初始化如下:
CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load";
CALL H2GIS_SPATIAL();
Run Code Online (Sandbox Code Playgroud)
在我的情况下,第一个命令成功执行,但第二个命令出现错误:
Syntax error in SQL statement "CREATE DOMAIN IF NOT EXISTS POINT AS GEOMETRY(1[*]) CHECK (ST_GEOMETRYTYPECODE(VALUE) = 1);"; SQL statement:
CREATE DOMAIN IF NOT EXISTS POINT AS GEOMETRY(1) CHECK (ST_GeometryTypeCode(VALUE) = 1); [42000-200] 42000/42000 (Help)
Run Code Online (Sandbox Code Playgroud)
这发生在执行registerGeometryType类的方法时org.h2gis.functions.factory.H2GISFunctions。由于某种原因,无法执行在该方法中编写的 SQL 语句。
到目前为止,我已经尝试通过将 SQL 命令放入src/main/resources/data.sql,并通过在 h2-console 中手动运行它们来初始化扩展。我还尝试使用不同版本的 H2GIS:1.4.0 和 1.5.0。所有的尝试都给我同样的效果。
当我尝试运行H2GIS 快速入门指南中所示的独立 H2GIS 时,它运行良好。
为了尽量减少问题的范围,我从头开始创建了一个最小的 Spring Boot 应用程序。这是我的build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.hibernate', name: 'hibernate-spatial', version: '5.4.10.Final'
compile group: 'org.orbisgis', name: 'h2gis', version: '1.5.0'
runtimeOnly 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)
我的application.properties:
spring.application.name=demo-h2gis
server.port=8080
spring.datasource.url=jdbc:h2:mem:demoh2gis
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.h2.console.enabled=true
spring.main.allow-bean-definition-overriding=true
Run Code Online (Sandbox Code Playgroud)
...以及应用程序本身:
@SpringBootApplication
@EnableAutoConfiguration
public class DemoH2gisApplication {
public static void main(String[] args) {
SpringApplication.run(DemoH2gisApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
可能是什么问题?
H2GIS 的版本只能与 H2 数据库的某些确切版本一起使用。对于 H2GIS 1.5.0,需要过时且不受支持的 H2 1.4.197,但您使用的是最新发布的 H2 1.4.200,而 H2GIS 1.5.0 在许多方面与它不兼容。您需要在build.gradle.
| 归档时间: |
|
| 查看次数: |
1282 次 |
| 最近记录: |