0

In a Spring Boot application (h2 in-memory database + Hibernate Spatial) in a Junit test, I'm getting error "Function "ST_WITHIN" not found" for a "within" query

pom.xml:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-spatial</artifactId>
</dependency>       

test/resources/application.properties:

spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=va
spring.jpa.database=h2  
spring.jpa.database-platform=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect

LocationRepository.java

@Query(value = "select l from Location l where within(l.position, :box) = true")
List<Location> findByPositionWithin(@Param("box") org.locationtech.jts.geom.Geometry box);

LocationEntity.java

@Column(columnDefinition = "GEOMETRY")
private org.locationtech.jts.geom.Point position;

Obviously, the alias ST_WITHIN is missing. Any ideas how to fix this?

2
  • Spatial functions for H2 are added by github.com/jdeolive/geodb. Commented Jun 8, 2020 at 19:37
  • H2 doesn't provide own spatial functions, there is an H2GIS extension for them and also some smaller projects. Hibernate Spatial uses GeoDB for a some weird reason, but GeoDB is basically unmaintaned and should be used with archaic versions of H2 without built-in GEOMETRY data type. You can try to use H2GIS with Hibernate Spatial instead with the same GeoDBDialect. Compatible versions of H2 and H2GIS need to be used, and H2GIS needs to be initialized with CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load"; CALL H2GIS_SPATIAL();. Commented Jun 9, 2020 at 4:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.