Spring Boot和JpaRepository按纬度和经度参数的距离排序

时间:2017-05-18 10:02:38

标签: postgresql spring-boot jpa-2.0 geospatial

使用JpaRepository接口创建查询时遇到问题。我使用Pageable机制。如何按距离添加排序? Event Place提供的地理参数。 (纬度,经度)。我想补充一下限制距离的能力。例如: "距离&lt ;: range"。 我希望距离"距离"字段,即@Transient。

EventRepository

@Repository
public interface EventRepository extends JpaRepository<Event, Long> {

public static final String GET_PAGE_QUERY = "WHERE " +
        "(:searchTerm is null or e.title like '%:searchTerm%') AND " +
        "((:userListId) is null or e.user.id in (:userListId)) AND " +
        "((:sportTypeIdList) is null or e.sportType.id in (:sportTypeIdList)) " ;


@Query(value = "SELECT e FROM Event e " + GET_PAGE_QUERY,
        countQuery = "SELECT COUNT(e) FROM Event e " + GET_PAGE_QUERY)
    Page<Event> getPage(@Param("searchTerm") String searchTerm, @Param("userListId") List<Long> userListId, @Param("sportTypeIdList") List<Long> sportTypeIdList, Pageable pageable);
}

事件

@Getter
@Setter
@Entity
@Table(name = "event")
public class Event {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "event_seq")
    @SequenceGenerator(name = "event_seq", sequenceName = "event_seq", allocationSize = 1)
    private Long id;

    @Column(name = "TITLE", length = 50, nullable = false)
    @NotNull
    @Size(min = 4, max = 50)
    private String title;

    @Column(name = "DESCRIPTION", length = 500, nullable = false)
    @NotNull
    @Size(min = 4, max = 500)
    private String description;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private SportType sportType;

    @OneToOne(fetch = FetchType.EAGER, optional = false)
    private EventPlace place;

    @ManyToOne(optional = false)
    private User user;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
    @Column(name = "CREATE_DATE", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @NotNull
    private Date createDate = new Date();

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
    @Column(name = "START_DATE", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @NotNull
    private Date startDate;

    @Column(name = "STATUS", nullable = false)
    @NotNull
    @Enumerated(EnumType.STRING)
    private EventStatus status = EventStatus.ACTIVE;

    @Transient
    private double distance;
}

EventPlace

@Getter
@Setter
@Entity
public class EventPlace {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "event_place_seq")
    @SequenceGenerator(name = "event_place_seq", sequenceName = "event_place_seq", allocationSize = 1)
    private Long id;

    @Column(name = "NAME", length = 50, nullable = false)
    @NotNull
    @Size(min = 4, max = 50)
    private String name;

    @Column(name = "CITY", length = 50, nullable = false)
    @NotNull
    @Size(min = 4, max = 50)
    private String city;

    @Column(name = "STREET", length = 50, nullable = false)
    @NotNull
    @Size(min = 4, max = 50)
    private String street;

    @Column(name = "ZIP_CODE", length = 50, nullable = false)
    @NotNull
    @Size(min = 4, max = 50)
    private String zipCode;

    @Column(name = "DESCRIPTION", length = 500, nullable = false)
    @NotNull
    @Size(min = 4, max = 500)
    private String description;

    @Column(name = "LATITUDE", nullable = false)
    @NotNull
    private Double latitude;

    @Column(name = "LONGITUDE", nullable = false)
    @NotNull
    private Double longitude;

}

0 个答案:

没有答案