如何配置Spring Boot项目以使用内存空间数据库进行测试?

7

以下是我的当前配置。我想在生产中使用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

测试中所有发现的内容均为 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。

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


你好,H2GIS目前不支持Hibernate。GeoDB是另一个针对旧版本H2的空间扩展。因此,对于你的东西,你应该使用GeoDB。 - nicolas-f
是的,我听说过geodb。它的一个问题是我找不到可以用作maven依赖项的任何地方。 - user1685095
你应该尝试加入geodb的邮件列表 https://groups.google.com/forum/#!forum/geodb - nicolas-f
2个回答

6
将GeoDBDialect与h2gis库结合在H2中可以正常工作。我可以存储和加载com.vividsolutions.jts.geom.Polygon而没有问题。
我正在使用Hibernate 5.2 + org.hibernate:hibernate-spatial:1.2.4 Hibernate方言:org.hibernate.spatial.dialect.h2geodb.GeoDBDialect 列类型:geometry
必须按照h2gis文档(https://github.com/orbisgis/h2gis)的描述初始化H2数据库。这些应该是初始化数据库时的最早的sql之一。
CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load";
CALL H2GIS_SPATIAL();

(H2GISFunctions 应该在类路径上。)


我可以确认,使用 Hibernate Spatial 5.0.12 和 org.orbisgis:h2gis-ext:1.3.2,同样适用于包含在 Spring Boot 1.5.10 中的 Hibernate 5.0.12。 - Wim Deblauwe

5
为了让其他人更容易地实现这一点,Mateusz Stefek的答案是正确的方法。以下是您需要确保postgis与您的hibernate模型和h2 db一起使用于单元测试用例的所有内容。请注意,以下内容将无法与hibernate 4一起使用,因此最好升级到版本5。请注意,在hibernate 5中改进的命名策略不再起作用,如果这使您感到困惑,您可以查看其他stackoverflow解决方案:ImprovedNamingStrategy在Hibernate 5中不再起作用 确保您具有以下依赖项:
hibernate空间+ 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;

以下内容确保Spring Boot能够序列化我们从Postgres中检索到的PostGIS几何数据,以便我们每次调用REST API端点时都可以使用。
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数据库中。
您的测试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文件中的hibernate方言指向GeoDBDialect

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

这对我有所帮助,因为我不得不停止使用org.opengeo:geodb,因为repo.boundlessgis.com不再可用,所以这帮助我转换到org.orbisgis:h2gis - Benjamin Vogler
谢谢 - 这个结合H2/H2GIS兼容性的另一个答案证明很有帮助。惊讶于似乎没有太多相关信息。 - Scott
感谢上帝! - David Lefarth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接