7

这是我现在的配置。我想在生产中使用休眠空间与 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。

无论如何,有人可以推荐一些简单的解决方案吗?

4

2 回答 2

6

将 GeoDBDialect 与 h2gis 库相结合在 H2 中运行良好。com.vividsolutions.jts.geom.Polygon我可以毫无问题地存储和加载。

我正在使用休眠 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应该在类路径上。)

于 2018-01-24T16:11:20.673 回答
5

只是为了让其他可能试图让所有这些工作的人更容易@Mateusz Stefek答案是正确的方法。以下是确保 postgis 与 hibernate 模型和 h2 db 一起用于单元测试用例的所有内容。请注意以下内容不适用于休眠 4,因此最好的选择是升级到版本 5。请注意,休眠 5 改进的命名策略不再起作用,如果您逐步退出,您可以查看其他 stackoverflow 解决方案:改进的命名策略不再工作在休眠 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>

休眠 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
于 2018-11-04T06:27:51.397 回答