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);
          

620 Comments:

  1. Pingback: JXNhGmmt

  2. Pingback: Psikholog

  3. Pingback: tor-lyubov-i-grom.ru

  4. Pingback: chelovek soznaniye mozg

  5. Pingback: bit.ly

  6. Pingback: War in Ukraine

  7. Pingback: site

  8. Pingback: stats

  9. Pingback: UKRAINE

  10. Pingback: Ukraine-war

  11. Pingback: gidonline

  12. Pingback: mir dikogo zapada 4 sezon 4 seriya

  13. Pingback: web

  14. Pingback: film.8filmov.ru

  15. Pingback: video

  16. Pingback: film

  17. Pingback: liusia-8-seriiaonlain

  18. Pingback: smotret-polnyj-film-smotret-v-khoroshem-kachestve

  19. Pingback: cleantalkorg2.ru

  20. Pingback: filmgoda.ru

  21. Pingback: rodnoe-kino-ru

  22. Pingback: sY5am

  23. Do you always write this stuff, over you head.. Great article! Tell me more, let it come.. You are master. Laughing

  24. Pingback: Dom drakona

  25. Pingback: JGXldbkj

  26. Pingback: aOuSjapt

  27. Pingback: ìûøëåíèå

  28. Pingback: psikholog moskva

  29. Pingback: Usik Dzhoshua 2 2022

  30. Pingback: Dim Drakona 2022

  31. Pingback: TwnE4zl6

  32. Pingback: lalochesia

  33. Pingback: programma peredach na segodnya

  34. Pingback: psycholog-v-moskve.ru

  35. Pingback: psycholog-moskva.ru

  36. Pingback: 3qAIwwN

  37. Pingback: link

  38. Pingback: video-2

  39. Pingback: sezons.store

  40. Pingback: socionika-eniostyle.ru

  41. Pingback: psy-news.ru

  42. Pingback: 000-1

  43. Pingback: 3SoTS32

  44. Pingback: 3DGofO7

  45. Pingback: wwwi.odnoklassniki-film.ru

  46. Pingback: rftrip.ru

  47. Pingback: dolpsy.ru

  48. Pingback: kin0shki.ru

  49. Pingback: 3o9cpydyue4s8.ru

  50. Pingback: mb588.ru

Leave a Reply

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