简体   繁体   English

如何配置spring-boot项目以与内存空间数据库一起进行测试?

[英]How to configure spring-boot project to work with inmemory spatial database for tests?

Here is my config now. 现在是我的配置。 I want to use hibernate spatial to work with postgis in production. 我想使用休眠空间在生产中使用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

For tests all found is h2gis project. 对于测试,所有找到的都是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;
    }

Not sure that it will work with geodbdialect or postgisdialect, altough it seems very close to postgisdialect. 不确定它将与geodbdialect还是postgisdialect一起使用,尽管看起来与postgisdialect非常接近。

Anyway can someone recommend some easy solution? 无论如何,有人可以推荐一些简单的解决方案吗?

Combining GeoDBDialect with h2gis library works fine in H2. 将GeoDBDialect与h2gis库结合使用在H2中效果很好。 I can store and load com.vividsolutions.jts.geom.Polygon with no problem. 我可以com.vividsolutions.jts.geom.Polygon存储和加载com.vividsolutions.jts.geom.Polygon

I'm using Hibernate 5.2 + org.hibernate:hibernate-spatial:1.2.4 我正在使用Hibernate 5.2 + org.hibernate:hibernate-spatial:1.2.4

Hibernate dialect: org.hibernate.spatial.dialect.h2geodb.GeoDBDialect 休眠方言: org.hibernate.spatial.dialect.h2geodb.GeoDBDialect

Column type: geometry . 列类型: geometry

H2 database should be initialized as described in the h2gis documentation ( https://github.com/orbisgis/h2gis ). H2数据库应按照h2gis文档( https://github.com/orbisgis/h2gis )中所述进行初始化。 These should be one of the first sql, when you initialize the database. 初始化数据库时,这些应该是第一个sql之一。

CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load";
CALL H2GIS_SPATIAL();

( H2GISFunctions should be on the classpath.) H2GISFunctions应该在类路径上。)

Just to make things easier for anyone else who may be trying to get all this to work @Mateusz Stefek answer is the correct approach to take. 只是为了使其他可能想要使所有这些工作正常工作的人更容易使用,@ Mateusz Stefek的答案是正确的方法。 Below is all you need to ensure postgis works with your hibernate models and h2 db for your unit test cases. 以下是确保Postgis与您的休眠模型和单元测试用例的h2 db一起使用所需的全部。 Take note below will not work with hibernate 4 so your best bet is to upgrade to version 5. Take note in hibernate 5 improved naming strategy doesnt work anymore if that phases you out you may have a look at other stackoverflow solutions: ImprovedNamingStrategy no longer working in Hibernate 5 请注意,以下内容不适用于hibernate 4,因此最好的选择是升级到版本5。请注意,在hibernate 5中,改进的命名策略不再起作用,如果逐步淘汰,您可以看看其他stackoverflow解决方案: EnhancedNamingStrategy不再起作用在Hibernate 5中

Ensure you have the following dependencies 确保您具有以下依赖关系

maven repos for hibernate spatial + h2gis 休眠空间+ 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 dependencies 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 model 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;

Below ensures spring boot can serialize our postgis geometry data from postgres when ever we call our REST API endpoints 下面确保了在我们调用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();
    }
}

If you use flyway you can enable it to run in your test script to ensure the folowing below gets executed on to your h2 db 如果使用flyway,则可以使其在测试脚本中运行,以确保将以下内容执行到h2 db

your test application.properties file 您的测试application.properties文件

flyway.url=jdbc:h2:mem:test;MODE=PostgreSQL;INIT=RUNSCRIPT FROM 'classpath:your_flyway_init.sql'

contents of your_flyway_init.sql script 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();

Ensure your test application.properties file hibernate dialet points to GeoDBDialect 确保您的测试application.properties文件休眠拨号指向GeoDBDialect

spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM