有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何配置springboot项目以使用inmemory空间数据库进行测试?

这是我现在的配置。我想使用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

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


共 (2) 个答案

  1. # 1 楼答案

    为了让其他任何想让这一切顺利进行的人更容易,@Mateusz Stefek answer是正确的方法。以下是确保postgis与hibernate模型和h2 db配合使用以进行单元测试用例所需的全部内容。请注意,下面的说明不适用于hibernate 4,因此最好升级到版本5。请注意,在hibernate 5中,改进的命名策略不再起作用。如果您逐渐退出,您可以看看其他stackoverflow解决方案:ImprovedNamingStrategy no longer working in Hibernate 5

    确保您具有以下依赖项

    hibernate spatial+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>
    

    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;
    

    下面确保了当我们调用RESTAPI端点时,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上执行以下内容

    您的测试应用程序。属性文件

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

    你的飞行路线初始的内容。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();
    

    确保您的测试应用程序。属性文件hibernate拨号指向GeoDBDialect

    spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
    
  2. # 2 楼答案

    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();
    

    H2GISFunctions应该在类路径上。)