-
-
Save VictorHaine/f9b5e7684fabf20ae0df6722b76bfa16 to your computer and use it in GitHub Desktop.
An example on how to use Hibernate-Spatial with Spring Data JPA Repositories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Entity | |
| @Table(name = "things") | |
| public class ThingEntity { | |
| @Id | |
| private Long id; | |
| // Needed for use with Hibernate Spatial 4.x | |
| // @Type(type = "org.hibernate.spatial.GeometryType") | |
| private Geometry geometry; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springframework.data.repository.Repository; | |
| import org.springframework.data.jpa.repository.Query; | |
| public interface ThingRepository extends Repository<ThingEntity, Long> { | |
| // This is a JPQL Query. Take note how I referenced the name of the entity, it's a Spring EL variable | |
| // If you refactor your entity, its name will be taken care of... | |
| @Query(value | |
| = "Select t from #{#entityName} t" | |
| + " where intersects(t.geometry, :area) = true" | |
| ) | |
| List<ThingEntity> findWithinArea(Geometry area); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment