Using the H2 gis, I could make my integration tests work with h2, creating spatial queries with jpa.
- Use the hiberante dialect org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
- Initialise the database by executing CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load"; CALL H2GIS_SPATIAL();
You can wrap the query in a sql script and use @Sql(scripts = "/sql/h2_gis_initialization.sql") to execute it on your test class.
- in maven, import h2 gis. Be aware that each version requires a specific version of h2
<!-- https://mvnrepository.com/artifact/org.orbisgis/h2gis -->
<dependency>
<groupId>org.orbisgis</groupId>
<artifactId>h2gis</artifactId>
<version>${h2.gis.version}</version>
</dependency>
- in your liquibase script, create the column with the GEOMETRY type:
<changeSet author="hschoonjans" id="create_table_localization_t">
<createTable tableName="LOCALIZATION_T">
<column name="ID" type="BIGINT">
<constraints primaryKey="true"/>
</column>
<column name="geometry" type="GEOMETRY"/>
</createTable>
</changeSet>
Run integration test:
@Test
void it_can_search_within_geometry() {
final Polygon polygon = new GeometryFactory().createPolygon();
List<Localization> results = repository.findAll(within(polygon));
Assertions.assertEquals(0, results.size());
}
public static Specification<Localization> within(Geometry geometry) {
return (root, query, builder) -> JTSSpatialPredicates.within(builder, root.get(Localization_.geometry), geometry);
}