Spring Data Pageable JPA Filter Query with 3 of 4 Parts of Composite Key via a Query Method

Let’s say you are using Spring Data and you have a “Composite Key” (ie, a tuple or EmbeddedId). Generally, you may want to query by the whole composite key (ie, by ID), which is generally fairly straightforward with a JPA “query method” (still not sure that is the official name). Even so, one of the reasons you use tuples or composite keys is to group data (and maybe even group within a group). Running with that thought, let’s say you have the following composite key:

 @NoArgsConstructor
 @AllArgsConstructor
 @Data
 @Embeddable
 public class PlantScheduleCompositeKey implements Serializable {
     @Column(name = "state")
     private String state;
     @Column(name = "city")
     private String city;
     @Column(name = "zip")
     private String zip;
     @Column(name = "crop")
     private String crop;
 } 

Next, let’s use the composite key in this Entity:

 @Entity
 @Data
 @Table(name="plant_schedule")
 public class PlantSchedule implements Serializable {
 
     @EmbeddedId
     private PlantScheduleCompositeKey id;

     private boolean frostDates;
     private boolean moonDates;
     private String sowSeedsIndoorSchedule;
     private String transplantSeedlingsSchedule;
     private String directSowSchedule;
     private String source;
 } 

Now, if you are using a CrudRepository, or better yet a PagingAndSortingRepository, you’ll already get findById() out of the box. This will not help us if we want to search by only city, state, and zip (ie, without the crop name). So, we need to craft a custom query method, which will be turned into JPQL behind the scenes.

 @RepositoryRestResource
 public interface IPlantScheduleRepository extends PagingAndSortingRepository<PlantSchedule, PlantScheduleCompositeKey>{

     public List<PlantSchedule> findAllByIdCityContainingAndIdStateContainingAndIdZipContaining(String city, String state, String zip, Pageable pageable);
 } 

The key here is to understand that there is a hierarchy here with a filter that is repeated for the 3 out of the 4 parts of the composite key we want to search against. We can interpret the substring IdCityContaining as being the field id on the entity PlantSchedule, followed by the part of the composite key we want to do a contains comparison against. Therefore, we repeat this pattern twice more for state and zip. So, in summary, we have a query method that will return all PlantSchedules with a city, state, and zip matching the provided arguments.

Sample call:

Pageable pageable = PageRequest.of(0, 100);
         List<PlantSchedule> pageList = plantScheduleRepository.findAllByIdCityContainingAndIdStateContainingAndIdZipContaining(city, state, zip, pageable);
          

250 Comments:

  1. Pingback: history-of-ukraine.ru news ukraine

  2. Pingback: newsukraine.ru

  3. Pingback: edu-design.ru

  4. Pingback: tftl.ru

  5. Pingback: brutv

  6. Pingback: site 2023

  7. Pingback: sitestats01

  8. Pingback: 1c789.ru

  9. Pingback: cttdu.ru

  10. Pingback: hdserial2023.ru

  11. Pingback: serialhd2023.ru

  12. Pingback: matchonline2022.ru

  13. Pingback: bit.ly/3OEzOZR

  14. Pingback: bit.ly/3gGFqGq

  15. Pingback: bit.ly/3ARFdXA

  16. Pingback: bit.ly/3ig2UT5

  17. Pingback: bit.ly/3GQNK0J

  18. Pingback: bep5w0Df

  19. Pingback: www

  20. Pingback: icf

  21. Pingback: 24hours-news

  22. Pingback: rusnewsweek

  23. Pingback: uluro-ado

  24. Pingback: irannews.ru

  25. Pingback: klondayk2022

  26. Pingback: tqmFEB3B

  27. Pingback: mangalib

  28. Pingback: x

  29. Pingback: 9xflix

  30. Pingback: xnxx

  31. Pingback: 123movies

  32. Pingback: kinokrad

  33. Pingback: batmanapollo

  34. Pingback: batmanapollo.ru - psychologist

  35. Pingback: batmanapollo psychologist

  36. Pingback: elizavetaboyarskaya.ru

  37. Pingback: vsovezdeisrazu

  38. Pingback: 2023

  39. Pingback: ipsychologos

  40. Pingback: yug-grib.ru

  41. Pingback: studio-tatuage.ru

  42. Pingback: bit.ly/pamfir-pamfir-2023-ua-pamfir

  43. Good article! We are linking to this great post on our website.
    Keep up the good writing.

  44. I like the valuable information you provide on your articles.
    I will bookmark your blog and check once more here regularly.

    I am rather sure I’ll be told lots of new stuff right here!
    Good luck for the following!

  45. You have made some decent points there. I looked on the internet for more information about the issue and found most people will go along with your
    views on this site.

  46. I loved as much as you will receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this increase.

  47. Hi! I could have sworn I’ve been to your blog before but after going through many of the articles I realized it’s new to me.
    Regardless, I’m certainly pleased I found it and I’ll be book-marking it
    and checking back frequently!

  48. I think this is among the most important info for me. And i’m glad reading your article. But should remark on few general things, The website style is wonderful, the articles is really excellent : D. Good job, cheers

  49. It’s actually very difficult in this full of activity life to listen news on Television, so I
    only use internet for that purpose, and get the most recent news.

  50. What’s up to every , as I am truly keen of reading this blog’s post to be
    updated daily. It includes good material.

Leave a Reply

Your email address will not be published. Required fields are marked *