这就是:当客户在网站上下订单时,他会传递他的邮政编码。在我的程序中,我收到了该邮政编码,获取了它的坐标,并从db返回最近的商店列表(按距离排序)。此外,我必须在数据库中存储最近的商店(仅限内部信息)
现在我有这样的想法
@Repository
public interface ClosestRepository extends JpaRepository<Closest,Long> {
@Query("SELECT new Closest(s, (6371 * acos(cos(radians(:latitude)) * cos(radians(ad.coordinates.latitude)) * cos(radians(ad.coordinates.longitude) - radians(:longitude)) + sin(radians(:latitude)) * sin(radians(ad.coordinates.latitude)))) as d) FROM Shop s JOIN s.address ad ORDER BY d")
List<Closest> findClosestOrderedByCoordinates(@Param("latitude") final double latitude, @Param("longitude") final double longitude);
}
最接近的实体是
@Entity
@Table(name = "closest")
public class Closest implements Comparable<Closest> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private Long id;
@OneToOne
@JoinColumn(name = "shop_id")
private Shop shop;
@Column(name = "zone")
@Enumerated(EnumType.STRING)
private Zone zone;
@Column(name = "distance")
private Double distance;
public Closest() {
}
public Closest(Shop shop, Double distance) {
this.shop = shop;
this.distance = distance;
zone = Zone.getByDistance(distance);
}
}
但这只适用于@JsonIgnore最近实体店铺。在内部它被正确设置和保存,但我无法获得带有嵌套商店信息的列表。 基于另一个实体创建实体列表是否可能/正确(就像我在repo中所做的那样)?
谢谢!