use*_*095 6 java spring h2 hibernate-spatial
这是我现在的配置。我想使用休眠空间在生产中使用 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
Run Code Online (Sandbox Code Playgroud)
对于所有发现的测试都是 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;
}
Run Code Online (Sandbox Code Playgroud)
不确定它是否适用于 geodbdialect 或 postgisdialect,尽管它似乎非常接近 postgisdialect。
无论如何,有人可以推荐一些简单的解决方案吗?
将 GeoDBDialect 与 h2gis 库结合在 H2 中工作正常。我可以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();
Run Code Online (Sandbox Code Playgroud)
(H2GISFunctions应该在类路径上。)
只是为了让任何可能试图让所有这些都发挥作用的人更容易@Mateusz Stefek 回答是正确的方法。以下是确保 postgis 与您的 hibernate 模型和 h2 db 一起用于您的单元测试用例所需的全部内容。请注意以下不适用于 hibernate 4,因此您最好的选择是升级到版本 5。请注意 hibernate 5 中改进的命名策略不再起作用,如果您逐步退出,您可以查看其他 stackoverflow 解决方案:ImprovedNamingStrategy 不再工作在休眠 5
确保您具有以下依赖项
用于休眠空间 + h2gis 的 maven repos
<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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
休眠 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;
Run Code Online (Sandbox Code Playgroud)
下面确保当我们调用 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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用 flyway,您可以启用它在您的测试脚本中运行,以确保以下内容在您的 h2 db 上执行
您的测试 application.properties 文件
flyway.url=jdbc:h2:mem:test;MODE=PostgreSQL;INIT=RUNSCRIPT FROM 'classpath:your_flyway_init.sql'
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
确保您的测试 application.properties 文件 hibernate dialet 指向 GeoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5502 次 |
| 最近记录: |