2012-08-23 109 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

看来,找到一个愚蠢的错误最好的方法是在张贴问题堆栈溢出。

我确定有测试数据可用。但事实证明,我必须非常确定,因为我实际上并没有添加任何内容。

当真的,真的是测试数据上面的查询工作正常。

我很想看到改进查询的方法,因为它对我的口味有点冗长,但是...