Friday, June 1, 2012

Spring Data JPA - Limit query size

This one is very informative and short:

If you are using Spring Data JPA, you are probably familiar with the technique of query creation out of methods names.

Following that mechanism, there is no explicit way to use the LIMIT keyword in the query name,
However, there is a simple way to use the SQL LIMIT with these queries.
The implicit way to limit the query result size is to utilize the pagination mechanism.
 
Simply provide and extra paging object with the objects number you need to limit.


Below is a simplified example of how the repository interface should be used:
public interface EntityRepository extends CrudRepository, JpaSpecificationExecutor {

 List findByEntityMemberNameLike(String query, Pageable pageable);//Pageable will limit the query result

}


Below is a simplified example of how the usage of such query could be:
@Autowired
EntityRepository entityRepository;

...

int queryLimit = 10;
List queryResults = entityRepository.findByEntityMemberNameLike(queryString, new PageRequest(0, queryLimit));



10 comments :

  1. @Gal Levinsky Thank you.. your answer helps me to solve a big problem

    ReplyDelete
  2. I really enjoyed this blog post, thank you for sharing it. I’ll return for more. See you soon!
    온라인경마
    경마사이트

    ReplyDelete
  3. I’m impressed, I must say. I’m here for the first time. Superb! I simply must tell you that I really love your blogs page. My boyfriend enjoys your blogs.야한동영상

    ReplyDelete
  4. As I apply more and more of these insights to my daily life I can see how your education has transformed you into a top expert in this industry. I talked about these very points to my professor today and she couldn’t argue any of these points. After seeing this I know I will not be the same. Be cool if you were close to me so we could hang. I don’t know why but this site takes almost a minute to fully load on my friends laptop. Reading your content is way better than snuggles.
    대딸방

    ReplyDelete
  5. Don’t be afraid to spread your thoughts. You have just made my year a great one! You are clearly an influence in this field so I'm sure many of these concepts probably come easily to someone like yourself however, I am still not understanding some things you’ve written here. What a highly descriptive and well written post. Any more feedback? It would be greatly appreciated. 출장안마

    ReplyDelete
  6. Good web site! I really love how it is simple on my eyes and the data are well written. I am wondering how I might be notified when a new post has been made. I have subscribed to your RSS feed which must do the trick! Have a nice day! 안마

    ReplyDelete