0
所以,我提出來存儲決策緯度和經度BigDecimals的如何查詢一個BigDecimal範圍JPA
但現在當我查詢了價值我想應留那麼一點點,如果我們查詢對於兩分之一米的距離,我們不會將它們視爲不同(我們指定的點總是至少相距100m)
以下條件查詢即使在填充時也不會選擇任何內容已知測試數據
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();
}
我毫不懷疑這個問題位於椅子和鍵盤之間。但我看不到它是什麼。
在此先感謝
測試環境是JPA2與Hibernate運行SQL 2008 Express的R2我的Windows 7的開發機器上