0

can we append the value of Query via QUERY from the string? if not, is there any other alternative?

NOTE: NEED TO RETURN DATA IN PAGE

@Query(nativeQuery = true, value = "select entity from table entity where 1=1" + query + "AND date =:date")

Page getSearchedTable(String query, @Param("date") LocalDate businessDate, Pageable pageable);

Sagar Saud
  • 124
  • 2
  • 11

1 Answers1

0

you can use below query to get pageable data. Keep few points in mind

  1. You must give space between equal sign and parameter like (data = :date)
  2. No need to give nativeQuery = true, you can use entity fields
  3. You can not use "query" as your variable in native query in spring boot. You must pass as a parameter from service.
  4. Just pass your query parameter in required query like select or as per your req. with below example.

@Query(value = "select entity from table entity where  1=1 AND date = :date AND query = :query")
Page<YourTableEntity> getSearchedTable(@Param("query") String query, @Param("businessDate") LocalDate businessDate, Pageable pageable);

However, if you have to mandatory use nativeQuery, then you can choose any of the following queries.


1. First option

 @Query(
      value = "select entity from table entity where  1=1 AND date = :date AND query = :query ORDER BY id", 
      countQuery = "SELECT count(*) FROM table", 
      nativeQuery = true)
      Page<YourTableEntity> getSearchedTable(@Param("query") String query, @Param("businessDate") LocalDate businessDate, Pageable pageable);

2. Second option

@Query(value = "select entity from table entity where  1=1 AND date = :date AND query = :query ORDER BY id \n-- #pageable\n", 
      countQuery = "SELECT count(*) FROM table",
      nativeQuery = true)
      Page<YourTableEntity> getSearchedTable(@Param("query") String query, 
      @Param("businessDate") LocalDate businessDate, Pageable pageable);
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
Chirag Shah
  • 353
  • 1
  • 13