第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

使用 Criteria api 查找半徑內(nèi)的坐標(biāo)

使用 Criteria api 查找半徑內(nèi)的坐標(biāo)

慕仙森 2023-06-04 16:51:50
我需要使用 CriteriaBuilder 從數(shù)據(jù)庫中獲?。ň暥群徒?jīng)度)距離給定緯度和經(jīng)度(用戶位置)一定距離(例如:10 公里)內(nèi)的所有行。在第三個(gè) if 中,我必須獲取結(jié)果并將其添加到謂詞中。        CriteriaBuilder cb = em.getCriteriaBuilder();        CriteriaQuery<Issue> cq = cb.createQuery(Issue.class);        Root<Issue> issue = cq.from(Issue.class);        List<Predicate> predicates = new ArrayList<>();        if (filters.getCategoryId() != null) {            Category issueCategory = categoryRepository.findById(filters.getCategoryId())                    .orElseThrow(() -> new ResourceNotFoundException("Category not found for this id :: " +String.valueOf(filters.getCategoryId())));            predicates.add(cb.equal(issue.get("category"), issueCategory));        }        if (filters.getPostedDays() != null) {            Date startDate = issueUtility.getStartDateForFilterIssues(filters.getPostedDays());            predicates.add(cb.between(issue.get("createdAt"), startDate,new Date()));        }        if (filters.getLatitude() != null && filters.getLongitude() != null) {              // fetch by radius code comes here        }        cq.where(predicates.toArray(new Predicate[0]));```
查看完整描述

1 回答

?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

得到了解決方案。


        CriteriaBuilder cb = em.getCriteriaBuilder();

        CriteriaQuery<Issue> cq = cb.createQuery(Issue.class);


        Root<Issue> issue = cq.from(Issue.class);

        List<Predicate> predicates = new ArrayList<>();


        if (filters.getCategoryId() != null) {

            Category issueCategory = categoryRepository.findById(filters.getCategoryId())

                    .orElseThrow(() -> new ResourceNotFoundException("Category not found for this id :: " +String.valueOf(filters.getCategoryId())));

            predicates.add(cb.equal(issue.get("category"), issueCategory));

        }

        if (filters.getPostedDays() != null) {

            Date startDate = issueUtility.getStartDateForFilterIssues(filters.getPostedDays());

            predicates.add(cb.between(issue.get("createdAt"), startDate,new Date()));

        }

        if (filters.getLatitude() != null && filters.getLongitude() != null) {

              // fetch by radius code comes here

            Expression<Issue> point1 = cb.function("point", Issue.class,

                    issue.get("latitude"),issue.get("longitude"));

            Expression<Double> point2 = cb.function("point", Double.class,

                    cb.literal(filters.getLatitude()),cb.literal(filters.getLongitude()));


            Expression<Number> distance = cb.function("ST_Distance_Sphere", Number.class,

                    point1,point2);

            predicates.add(cb.lt(distance,30000));

        }

        cq.where(predicates.toArray(new Predicate[0]));

        return em.createQuery(cq).getResultList();

    }


查看完整回答
反對 回復(fù) 2023-06-04
  • 1 回答
  • 0 關(guān)注
  • 141 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號