Спецификация / Предикат для Hibernate / пространственный, чтобы отфильтровать объект, находится ли он в радиусе или нет - PullRequest
1 голос
/ 08 апреля 2020

Я хотел бы переписать следующий запрос в Предикате / Спецификации, чтобы я мог связать их.

этот запрос фильтрует все мои определенные объекты OptEvent в указанной c области

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

обычно я пишу такую ​​спецификацию и позже могу связать их вместе. В зависимости от того, применен фильтр или нет.

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() + "%");

            }
        };
    }

дополнительная информация о моем окружении. Мой пом ниже. Я использую Postgres и 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>

для своей точки местоположения. Я использую 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() {

    }

1 Ответ

1 голос
/ 24 апреля 2020

Теперь я нашел ответ сам.

public static Specification<Event> filterWithinRadius(double longitute, double latitude, double radius) {
    return new Specification<Event>() {
        @Override
        public Predicate toPredicate(Root<Event> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            GeometryFactory factory = new GeometryFactory();
            Point comparisonPoint = factory.createPoint(new Coordinate(latitude, longitute));
            return SpatialPredicates.distanceWithin(builder, root.get("location"), comparisonPoint, radius);
        }
    };
}

Важные SpatialPredicates впервые отсутствовали в версии 5.4.10 FINAL, поэтому мне пришлось довести зависимость до 5.4.14 FINAL.

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>5.4.14.Final</version>
    </dependency>

Чтобы перестроить функцию 1 в 1, как в начальном вопросе, следующая структура является правильной. В противном случае преобразование из геометрии в географию отсутствует

public static Specification<Event> filterWithinRadius(double longitute, double latitude, double radius) {
    return new Specification<Event>() {
        @Override
        public Predicate toPredicate(Root<Event> root, CriteriaQuery<?> query, CriteriaBuilder builder) {

            Expression<Geometry> geography = builder.function("geography", Geometry.class, root.get("location"));
            Expression<Point> point = builder.function("ST_Point", Point.class, builder.literal(longitute),
                    builder.literal(latitude));
            Expression<Point> comparisonPoint = builder.function("ST_SetSRID", Point.class, point,
                    builder.literal(4326));
            Expression<Boolean> expression = builder.function(SpatialFunction.dwithin.toString(), boolean.class,
                    geography, comparisonPoint, builder.literal(radius));
            return builder.equal(expression, true);
        }
    };
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...