#Pageable with multiple tables

7 messages · Page 1 of 1 (latest)

broken jackal
#

I have 4 entitles, 1 of them, books, has a relationship between the remaining 3 entities. my FK are ids, when I query the data trough the repository and sort it using Pageable, it somewhat works, it works if I sort it by the attributes in the books table, but when I try to sort it by one of the attributes in the relationship table, it doesn't work. I think it's because it's sorting by the ids and not by the values.

@Repository
public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor<Book> {
    @Query("SELECT b FROM Book b JOIN FETCH b.language JOIN FETCH b.genre JOIN FETCH b.author")
    Page<Book> findAllBookDetails(Pageable pageable);
}
mint kettleBOT
#

This post has been reserved for your question.

Hey @broken jackal! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

broken jackal
#
@Service
@AllArgsConstructor
public class BookServiceImpl implements BookService {
    public static final String DEFAULT_SORT_COLUMN = "id";
    private BookDao bookDao;

    @Override
    public PaginatedResponseDto getBooks(BookSearchCriteria criteria) {
        Pageable pageable = createPageable(criteria);
        Page<Book> bookPage = bookDao.findAll(pageable);
        List<BookResponseDto> dtoList = BookMapper.toDto(bookPage.getContent());

        return createPaginatedResponse(dtoList, bookPage);
    }

    private Pageable createPageable(BookSearchCriteria criteria) {
        int pageOffset = criteria.getOffset() != null ? criteria.getOffset() : 0;
        int pageLimit = criteria.getLimit() != null ? criteria.getLimit() : Integer.MAX_VALUE;
        Sort sortOrder = determineSortOrder(criteria);

        return PageRequest.of(pageOffset, pageLimit, sortOrder);
    }

    private Sort determineSortOrder(BookSearchCriteria criteria) {
        String sortColumn = criteria.getSortColumn() != null ? criteria.getSortColumn() : DEFAULT_SORT_COLUMN;
        return criteria.getSortOrder() == SortOrder.ASC ? Sort.by(sortColumn).ascending() : Sort.by(sortColumn).descending();
    }

    private PaginatedResponseDto createPaginatedResponse(List<BookResponseDto> dtoList, Page<Book> bookPage) {
        return new PaginatedResponseDto(
                dtoList,
                bookPage.getNumber(),
                bookPage.getTotalPages(),
                bookPage.getTotalElements()
        );
    }

    @Override
    public Book saveBook(BookRequestDto bookRequestDto) {
        return bookDao.save(BookMapper.toEntity(bookRequestDto));
    }

    @Override
    public Book getBookById(Long id) {
        return bookDao.getById(id);
    }

    @Override
    public void deleteBook(Long id) {
        bookDao.deleteById(id);
    }
}
mint kettleBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

fiery lotus
#

Please show the entities code.

#

Oh, I think the field bookDao will always be null. You need to inject or set the field bookDao!!!