Joining pageable tables without relation using JPA criteria

77 Views Asked by At

I have two tables with no modeled relations. in my custom repository like below:

Method one

public Page<HistoryTable> findInquiryhistory(InquiryRequest req, int pageNumber, int pageSize){
    Pageable pageable = PageRequest.of(pageNumber, pageSize);
    return historyRepository.findAll(new Specification<HistoryTable>() {
        @Override
        public Predicate toPredicate(Root<HistoryTable> root, CriteriaQuery<?> query,
                                     CriteriaBuilder criteriaBuilder) {
            List<Predicate> predicates = new ArrayList<>();
            filterByCarId(predicates,criteriaBuilder, root,
                    req.getCarId());
            filterByCarnameId(predicates, criteriaBuilder, root,
                    req.getCarnameId());
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    }, pageable);
}

Method two

public Page<DataTable> findInquiry(InquiryRequest req, int pageNumber, int pageSize) {
    Pageable pageable = PageRequest.of(pageNumber, pageSize);
    return dataRepository.findAll(new Specification<DataTable>() {
        @Override
        public Predicate toPredicate(Root<DataTable> root, CriteriaQuery<?> query,
                                     CriteriaBuilder criteriaBuilder) {
            List<Predicate> predicates = new ArrayList<>();

            filterByCarId(predicates,criteriaBuilder, root,
                    req.getCarId());
            filterByCarnameId(predicates, criteriaBuilder, root,
                    req.getCarnameId());
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));

        }
    }, pageable);
}

Is there any possible way join above two method into one code using join, both tables contain groupId as a forign key but its not model relation just relation id wise.is there any possible way to connect above two method into one method use groupId as fk cause i need both in one resultset .Thanks

0

There are 0 best solutions below