我需要使用 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]));```
繁星coding
相关分类