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的开发机器上