2012-08-23 47 views
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的開發機器上

回答

1

看來,找到一個愚蠢的錯誤最好的方法是在張貼問題堆棧溢出。

我確定有測試數據可用。但事實證明,我必須非常確定,因爲我實際上並沒有添加任何內容。

當真的,真的是測試數據上面的查詢工作正常。

我很想看到改進查詢的方法,因爲它對我的口味有點冗長,但是...