映射要选择的命名查询的实体字段



我的实体确实包含地理位置。在我的存储库中,我正在进行自定义查询,以获取给定位置特定半径内的所有消息。当然,根据给定点的不同,每个查询的距离不同。所以我不想坚持这段距离。我需要在查询中计算距离。我想将这个计算的距离添加到我的实体中。有人知道我如何在不破坏实体字段的情况下将结果集中的内容映射到实体字段吗?我知道@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;

使用spring数据jpa投影来获得所需:

// 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(...);

最新更新