Skip to content

Instantly share code, notes, and snippets.

@VictorHaine
Forked from michael-simons/ThingEntity.java
Created January 27, 2020 02:44
Show Gist options
  • Select an option

  • Save VictorHaine/f9b5e7684fabf20ae0df6722b76bfa16 to your computer and use it in GitHub Desktop.

Select an option

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
@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;
}
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