[英]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.