“我的实体”确实包含地理位置。 在我的存储库中,我正在执行自定义查询,以获取给定位置的特定半径内的所有消息。当然,每个查询的距离取决于给定的点。所以我不想坚持距离。 我需要在查询中计算距离。我想将此计算出的距离添加到我的实体中。有人知道我如何在不持久的情况下将结果集中的内容映射到实体字段。我知道有@Forumla注释可以做一些简单的事情,但这对我没有帮助。
查询:
@Query(name = "MessageWithDistance",value = "SELECT m.*,cast(st_distance_sphere(m.location,:userLocation) as double precision) as distance FROM message_entity AS m WHERE st_within(m.location, :polygon)=TRUE",nativeQuery = true)
List<MessageEntity> findWithin(@Param("userLocation") Point point, @Param("polygon") Polygon polygon);
@Entity
public class MessageEntity {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY,generator = "message_seq_gen")
@SequenceGenerator(name = "message_seq_gen", sequenceName = "message_id_seq")
private Long id;
private LocalDateTime dateTime;
@Column(name = "location",columnDefinition = "GEOMETRY(Point, 4326)")
@Type(type = "jts_geometry")
private Point location;
private String message;
@Transient
private Double distance;
答案 0 :(得分:1)
使用spring-data-jpa projection来获得想要的东西:
// define the dto interface
public interface MessageEntityDto {
Long getId();
LocalDateTime getDateTime();
Point getLocation();
String getMessage();
Double getDistance();
}
@Query(value = "SELECT m.*, ... as distance FROM message_entity AS m ..., nativeQuery = true)
List<MessageEntityDto> findWithin(...);