I would like to rewrite the following query in a Predicate/Specification so that i can link them.
this query filters all my defined OptEvent Entitys within a specific area
@Query(value = "SELECT * FROM opt_event WHERE ST_DWithin(cast(opt_event.locationpoint as geography),ST_SetSRID(ST_Point(?2, ?1),4326), 100000);", nativeQuery = true)
public Set<OptEvent> findAllEventsInRange(double longitude, double latitude);
normally I write a specification like this, and can string them together later. Depending on whether a filter is applied or not.
public static Specification<OptEvent> filterArea(Double longitude, Double latitude) {
return new Specification<OptEvent>() {
@Override
public Predicate toPredicate(Root<OptEvent> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
SpatialRelateExpression geomExpression = SpatialRestrictions.within("locationpoint ", area);
//this is the only thing i could find, but i have no idea if its the right path or how i can transform it to the Specification
// what is area ?
//Example when i filter for the destination in the OptEvent
//return (root, query, builder) -> builder.like(builder.upper(root.get("destination")),
//"%" + destination.toUpperCase() + "%");
}
};
}
further information regarding my environment. My pom below. I use Postgres and Hibernate 5.4.10.Final.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
//Version 5.4.10 FINAL from Hibernate-Core
</dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.13</version>
</dependency>
</dependencies>
for my location point i use org.locationtech.jts.geom.Point;
@Entity
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class OptEvent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String destination;
private String description;
private int year;
private EventSeries eventSeries;
private LocalDate eventDate;
@JsonSerialize(using = GeometrySerializer.class)
@JsonDeserialize(contentUsing = GeometryDeserializer.class)
private Point locationpoint;
@OneToMany(mappedBy = "event", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties("event")
private Set<OptResult> results = new HashSet<>();
public OptEvent() {
}