0

そこで、緯度と経度を 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 です。

4

1 に答える 1

1

愚かなエラーを見つける最善の方法は、StackOverflow に質問を投稿することです。

テストデータが利用可能であると確信していました。しかし、実際には何も追加していなかったので、本当に確認する必要がありました.

本当に、本当にテスト データがある場合、上記のクエリは正常に機能します。

私の好みでは少し冗長なので、クエリを改善する方法についての意見を知りたいのですが...

于 2012-08-23T14:30:22.107 に答える