I have a requirement while querying a database using pagination. While pagination, I will give page size and page index to the query like shown below,
select distinct tp.* from kat_task_property tp inner join kat_task_to_workstream ttw on ttw.ttw_frn_task_id = tp.tp_frn_task_id and ttw.ttw_frn_workstream_id= :workStreamId and ttw.ttw_ended_by is null and tp.tp_ended_by is null and tp.tp_is_active=true and ttw.ttw_is_active=true left join kat_user_to_task_order kto on ttw.ttw_id = kto.uto_frn_task_to_workstream_id and kto.uto_frn_user_id = :userId order by tp.tp_completed_at ,kto.uto_order limit :index, :size
The sample result will be,
tp_id tp_completed_at
1 2017-02-27 06:47:52
2 null
3 null
4 2017-03-14 12:59:24
5 null
6 null
7 null
My requirement is when index is 0 in the query, I should get all the data where tp_completed_at is null irrespective of the value size has in the query. I mean, pagination should not be applied when index is zero and I should get all the entries with tp_completed_at is null. And, when index has value other than 0 pagination should apply. Please Help
you can use Pageable to do that. PageRequest(page, size)
Pageable page = new PageRequest(0, 10);
for example :
Page<Incident> findByProblemId(Long problemId, Pageable page);
In your case , countQuery = ".... count(distinct tp.*) ... without limit"
@Query(value = "your existing Query without limit ", countQuery = "countQuery", nativeQuery = true)
getData(@Param("workStreamId") String workStreamId, @Param("userId") String userId, Pageable page);
You can achieve this by executing different queries based on the index value.
Edit: Try this hack, Works if your result set is less than Integer.MAX_VALUE
@Query("select distinct tp.* from kat_task_property tp inner join kat_task_to_workstream ttw on ttw.ttw_frn_task_id = tp.tp_frn_task_id and ttw.ttw_frn_workstream_id= :workStreamId and ttw.ttw_ended_by is null and tp.tp_ended_by is null and tp.tp_is_active=true and ttw.ttw_is_active=true left join kat_user_to_task_order kto on ttw.ttw_id = kto.uto_frn_task_to_workstream_id and kto.uto_frn_user_id = :userId and order by tp.tp_completed_at, kto.uto_order")
Page<T> findTp(Pageable pageable);
PagingAndSortingRepository<YourObject, Long> repository = // … get access to a bean
if(index == 0)
Page<YourObject> yourObject= repository.findTp(new PageRequest(index, Integer.MAX_VALUE));
else
Page<YourObject> yourObject= repository.findTp(new PageRequest(index, size));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.