简体   繁体   中英

Native query is returning all the records for the #pageable in spring boot

I gone through this in stackoverflow for the pagination in native queries, No error while returning records. It is giving all records instead of taking parameter value in pageable ex: iam passing these pageSize(5 or 4) and pageNo(0) to pageable parameter.

@Query(value = "select * from job_post where comp_addr_ref_id not in"
            + "(select mapping_id from company_address where ref_company_id in"
            + "(select mapping_id from  company where mapping_id in"
            + "(select comp_ref_id from comp_visibility where is_comp_visible=0 and login_ref_id=:mappingId)))"
            + " order by created_date desc  \n#pageable\n", nativeQuery = true)
    public List<JobPost> findUserBlockedCompanies(String mappingId, Pageable pageable);

If you're using Spring Boot 2.0 or higher, try to delete \\n#pageable\\n section.

If you use PostgreSql and MySql change the value \\n#pageable\\n to ?#{#pageable}

If it still doesn't work you can enable logging of SQL statements using spring.jpa.show-sql=true in your application.properties

I had similar issue. If you are using Spring Data prior to 2.0.4, then you can try to replace

\\n#pageable\\n

with

\\n /* --#pageable */ \\n

This works for Postgres and Oracle.

PS In Spring Data 2.0.4+ you can completely remove this pageable token from query as it's not required anymore ( https://jira.spring.io/browse/DATAJPA-928 )

See also Spring Data and Native Query with pagination

I have fixed in oracle. My way is:

1-> write order by created_date OFFSET:offset ROWS FETCH NEXT:pageSize ROWS ONLY to your main query

2-> create count query for your main query

3-> and you can unit your datas with new PageIml<>(mainQueryResult, PageRequest.of(page,size), countQueryResult);

it works for all dbs

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM