Spring 映射要选择的命名查询的实体字段
我的实体确实包含地理位置。 在我的存储库中,我正在进行自定义查询,以获取给定位置特定半径内的所有消息。当然,根据给定的点,每个查询的距离都不同。所以我不想再坚持下去了。 我需要在查询中计算距离。我想将此计算的距离添加到实体中。有人知道我如何将结果集中的某个内容映射到实体字段而无需验证它吗。我知道有@Forumla注释可以做一些简单的事情,但这对我没有帮助 查询:Spring 映射要选择的命名查询的实体字段,spring,spring-boot,spring-data-jpa,geospatial,postgis,Spring,Spring Boot,Spring Data Jpa,Geospatial,Postgis,我的实体确实包含地理位置。 在我的存储库中,我正在进行自定义查询,以获取给定位置特定半径内的所有消息。当然,根据给定的点,每个查询的距离都不同。所以我不想再坚持下去了。 我需要在查询中计算距离。我想将此计算的距离添加到实体中。有人知道我如何将结果集中的某个内容映射到实体字段而无需验证它吗。我知道有@Forumla注释可以做一些简单的事情,但这对我没有帮助 查询: @Query(name = "MessageWithDistance",value = "SELECT m.*,cast(st_dis
@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;
用于获得您想要的:
// 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(...);
谢谢你的快速回复,。不幸的是,我得到以下JDBC类型无方言映射的异常:1111