Problem:
I have a couple of tables TIMESHEET(id, start_date, end_date) and TIMESHEET_ENTRIES(id, ts_id, enrty_date, start_time, end_time) -linked by TIMSHEET.ID as foreign key in TIMESHEET_ENTRIES. So for same TIMESHEET we can have multiple timesheet entries.
Objective is to get all the timesheet and its entries by applying pagination on the timesheet
Example: request with size=5 Should return all DB entries that represent 5 distinct timesheets irrespective of count of entries.
Currently I am using native query like this:
@Query(value = "SELECT ts.id as id, ts.start_date AS startDate, ts.end_date as endDate, tse.id AS entryId, tse" +
".entry_date AS entryDate, tse.start_time AS startTime, tse.end_time AS endTime FROM time_sheets ts " +
"INNER JOIN time_sheet_entries tse ON ts.id = tse.ts_id WHERE ts.start_date=:startDate",
countQuery ="select count(*) FROM time_sheets ts INNER JOIN time_sheet_entries tse ON ts.id = tse.ts_id " +
"WHERE ts.start_date=:startDate",
nativeQuery = true)
Page<TimeSheetDetailsView> findAll(@Param("startDate") LocalDate startDate, Pageable pageable);
This returns paged data based on the total rows count i.e. timesheet entries count. Is it possible to implement pagination size check based on unique ts.id using spring data.
Solutions that I have thought of:
- Get all data and then implement pagination using PageImpl. This would still get all the data from DB which I don't want
- Implement pagination on timesheets table data, and then get timesheet entries data for each timesheet implementation. There will be lots of DB calls in this case, which is also not idle.
Any better solution is will be helpful.