Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用条件api查找半径内的坐标_Java_Mysql_Hibernate_Jpa_Criteria Api - Fatal编程技术网

Java 使用条件api查找半径内的坐标

Java 使用条件api查找半径内的坐标,java,mysql,hibernate,jpa,criteria-api,Java,Mysql,Hibernate,Jpa,Criteria Api,我需要使用CriteriaBuilder从数据库中获取所有行,其中(纬度和经度)与给定纬度和经度(用户位置)之间的距离(例如:10公里) 在第三个if中,我必须获取结果并将其添加到谓词中 CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Issue> cq = cb.createQuery(Issue.class); Root<Issue> issue

我需要使用CriteriaBuilder从数据库中获取所有行,其中(纬度和经度)与给定纬度和经度(用户位置)之间的距离(例如:10公里)

在第三个if中,我必须获取结果并将其添加到谓词中

        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]));```
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(Issue.class);
根问题=cq.from(问题类);
列表谓词=新的ArrayList();
if(filters.getCategoryId()!=null){
Category issueCategory=categoryRepository.findById(filters.getCategoryId())
.OrelsThrow(()->new ResourceNotFoundException(“未找到此id的类别::”+String.valueOf(filters.getCategoryId())));
add(cb.equal(issue.get(“category”)、issueCategory);
}
if(filters.getPostedDays()!=null){
Date startDate=issueUtility.getStartDateForFilterIssues(filters.getPostedDays());
add(cb.between(issue.get(“createdAt”)、startDate、newdate());
}
if(filters.getLatitude()!=null&&filters.getLatitude()!=null){
//按radius代码取回到这里
}
cq.where(谓词toArray(新谓词[0])```
找到了解决方案

        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();
    }
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(Issue.class);
根问题=cq.from(问题类);
列表谓词=新的ArrayList();
if(filters.getCategoryId()!=null){
Category issueCategory=categoryRepository.findById(filters.getCategoryId())
.OrelsThrow(()->new ResourceNotFoundException(“未找到此id的类别::”+String.valueOf(filters.getCategoryId())));
add(cb.equal(issue.get(“category”)、issueCategory);
}
if(filters.getPostedDays()!=null){
Date startDate=issueUtility.getStartDateForFilterIssues(filters.getPostedDays());
add(cb.between(issue.get(“createdAt”)、startDate、newdate());
}
if(filters.getLatitude()!=null&&filters.getLatitude()!=null){
//按radius代码取回到这里
表达式point1=cb.function(“point”,Issue.class,
issue.get(“纬度”)、issue.get(“经度”);
表达式point2=cb.function(“点”,Double.class,
literal(filters.getLatitude()),cb.literal(filters.getLatitude());
表达式距离=cb.函数(“ST_distance_Sphere”,Number.class,
第1点、第2点);
add(cb.lt(distance,30000));
}
cq.where(谓词toArray(新谓词[0]);
返回em.createQuery(cq.getResultList();
}

Ok。那么问题出在哪里?@mytwoons如何使用标准构建器计算与坐标的距离请看这里@NikolayShevchenko Mysql