java - Limiting the number of results in Spring data JPA -
i have 20+ tables in db , need have method fetch non-deleted rows tables. therefore, wrote following interface:
@norepositorybean public interface commonrepositry<t, id extends serializable> extends crudrepository<t, uuid>{ list<t> findbyisdeleted(boolean isdeleted); } all model repositories extend commonrepositry , i'm able access non-deleted rows:
public interface modelnamerepository extends commonrepositry<modelname, uuid> { // sevral spring jpa methods as tables growing in size, want put adjustable limit on number of results method returns. there 2 ways , i'm having issues both of them:
from docs, how supposed it:
list<user> findtop1000byisdeleted(boolean isdeleted); it returns me top 1000 rows.
problem: want externalise 1000 properties file if have change, @ single place.
another way use @query annotation , write sql query:
@query(value = "select * table_name" + " is_deleted = :isdeleted limit 1000;", nativequery = true) list<t> findbyisdeleted(@param("isdeleted")boolean isdeleted); problem: how table_name? model class names table-names have simple camelcase camel_case mapping. how model class name t?
problem 1: can pass second parameter query, called pageable.
list<t> findbyisdeleted(boolean isdeleted, pageable pageable);
this pageable interface, can use pagerequest implementation it. requires 2 things create pagerequest, page number , size (meaning how many rows want fetch). of course, in case page number 0 , size should set reading value properties file.
problem 2: can externalize table name class consists of constant values. can use these values in @table annotation on entity class , here well.
more reading: http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.special-parameters
Comments
Post a Comment