[英]How to configure spring-boot project to work with inmemory spatial database for tests?
现在是我的配置。 我想使用休眠空间在生产中使用Postgis。
spring:
profiles: production
datasource:
platform: postgres
url: jdbc:postgresql://192.168.99.100:5432/dragon
username: dragon
password: dragon
database:
driverClassName: org.postgresql.Driver
jpa:
database: POSTGRESQL
database-platform: org.hibernate.spatial.dialect.postgis.PostgisDialect
show-sql: true
hibernate:
ddl-auto: update
---
spring:
profiles: development
datasource: SpatialInMemoryDb
jpa:
database-platform: org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
hibernate:
ddl-auto: create-drop
对于测试,所有找到的都是h2gis项目。
public class SpatialInMemoryDb extends SingleConnectionDataSource{
public SpatialInMemoryDb() {
setDriverClassName("org.h2.Driver");
setUrl("jdbc:g2:mem:test");
setSuppressClose(true);
}
@Override
public Connection getConnection() throws SQLException {
System.out.println("************");
Connection connection = super.getConnection();
try (Statement st = connection.createStatement()) {
// Import spatial functions, domains and drivers
// If you are using a file database, you have to do only that once.
CreateSpatialExtension.initSpatialExtension(connection);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
不确定它将与geodbdialect还是postgisdialect一起使用,尽管看起来与postgisdialect非常接近。
无论如何,有人可以推荐一些简单的解决方案吗?
将GeoDBDialect与h2gis库结合使用在H2中效果很好。 我可以com.vividsolutions.jts.geom.Polygon
存储和加载com.vividsolutions.jts.geom.Polygon
。
我正在使用Hibernate 5.2 + org.hibernate:hibernate-spatial:1.2.4
休眠方言: org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
列类型: geometry
。
H2数据库应按照h2gis文档( https://github.com/orbisgis/h2gis )中所述进行初始化。 初始化数据库时,这些应该是第一个sql之一。
CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load";
CALL H2GIS_SPATIAL();
( H2GISFunctions
应该在类路径上。)
只是为了使其他可能想要使所有这些工作正常工作的人更容易使用,@ Mateusz Stefek的答案是正确的方法。 以下是确保Postgis与您的休眠模型和单元测试用例的h2 db一起使用所需的全部。 请注意,以下内容不适用于hibernate 4,因此最好的选择是升级到版本5。请注意,在hibernate 5中,改进的命名策略不再起作用,如果逐步淘汰,您可以看看其他stackoverflow解决方案: EnhancedNamingStrategy不再起作用在Hibernate 5中
确保您具有以下依赖关系
休眠空间+ h2gis的Maven仓库
<repository>
<id>OSGEO GeoTools repo</id>
<url>http://download.osgeo.org/webdav/geotools</url>
</repository>
<repository>
<id>Hibernate Spatial repo</id>
<url>http://www.hibernatespatial.org/repository</url>
</repository>
Maven依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.orbisgis</groupId>
<artifactId>h2gis-functions</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
Hibernate JPA模型
import com.vividsolutions.jts.geom.Polygon;
/**
* setting columnDefintion = "geometry(Polygon,4326)" will not work as h2gis
* expects default type geometry not an explicit defintion of the actual type * point
* polygon, multipolygon etc
*/
@Column(name = "region_boundary", nullable = false, columnDefinition = "geometry")
private Polygon regionBoundary;
下面确保了在我们调用REST API端点时,Spring Boot可以从postgres序列化我们的postgis几何数据。
import com.bedatadriven.jackson.datatype.jts.JtsModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JacksonConfig {
@Bean
public JtsModule jtsModule() {
return new JtsModule();
}
}
如果使用flyway,则可以使其在测试脚本中运行,以确保将以下内容执行到h2 db
您的测试application.properties文件
flyway.url=jdbc:h2:mem:test;MODE=PostgreSQL;INIT=RUNSCRIPT FROM 'classpath:your_flyway_init.sql'
your_flyway_init.sql脚本的内容
CREATE SCHEMA IF NOT EXISTS "{your_schema_if_applicable}";
CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load";
CALL H2GIS_SPATIAL();
确保您的测试application.properties文件休眠拨号指向GeoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.