そこで、緯度と経度を BigDecimals として保存することにしました
しかし、値をクエリするときは、少し余裕を持たせて、メートル離れた 2 つのポイントをクエリしても異なるものとして扱わないようにします (指定するポイントは常に少なくとも 100 m になります)。離れて)
以下の基準クエリでは、既知のテスト データを入力しても何も選択されません。
public List<Location> findLocations(BigDecimal latitude, BigDecimal longitude) {
BigDecmial geoPointDifferenceThreshold = new BigDecimal(0.0001).setScale(12, RoundingMode.HALF_EVEN);
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Location> cq =cb.createQuery(Location.class);
Root<Location> locationRoot = cq.from(Location.class);
Path<BigDecimal> latitudePath = locationRoot.<BigDecimal>get("latitude");
Path<BigDecimal> longitudePath = locationRoot.<BigDecimal>get("longitude");
Predicate latitudeBetweenRange = cb.between(latitudePath, latitude.subtract(geoPointDifferenceThreshold), latitude.add(geoPointDifferenceThreshold));
Predicate longitudeBetweenRange = cb.between(longitudePath, longitude.subtract(geoPointDifferenceThreshold), longitude.add(geoPointDifferenceThreshold));
cq.select(locationRoot).where(latitudeBetweenRange); //within 11m
cq.select(locationRoot).where(longitudeBetweenRange); //within 11m
return em.createQuery(cq).getResultList();
}
問題が椅子とキーボードの間にあることは間違いありません。しかし、私はそれが何であるかを見ることができません。
前もって感謝します
テスト環境は、SQL 2008 Express R2 を実行している Windows 7 開発マシンで Hibernate を使用した JPA2 です。