I need to fetch all the rows from database where the (latitude and longitude) are within a distance(ex: 10 Km) from a given latitude and longitude (user location) using CriteriaBuilder.
in the third if , I have to fetch the results and add it to predicate.
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]));```