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: buy raw garden cartridges online

  2. Pingback: happyLuke

  3. Pingback: testosterone booster

  4. Pingback: Rent a car Bar

  5. Pingback: Buy Vyvanse Online UK

  6. Pingback: ipollo b2 for sale

  7. Pingback: Buy Banana Diesel Strain of Marijuana Online,

  8. Pingback: CableFreeTV

  9. Pingback: Budget Gorilla Trekking Uganda

  10. Pingback: Artvin Gazette

  11. Pingback: happyruck

  12. Pingback: virtual card buy with cryptocurrency

  13. Pingback: hennessy white for sale online

  14. Pingback: e-liquids

  15. Pingback: best solar generators

  16. Pingback: Panache Academy, Vadodara

  17. Pingback: the last song

  18. Pingback: search

  19. Pingback: Psilocybe Cubensis

  20. Pingback: walther arms ppq m2 subcompact 9mm 10rd & 15rd 3.5

  21. Pingback: browning a5 sweet 16 manual

  22. Pingback: beretta 84 380 acp

  23. Pingback: Buy GBL wheels cleaner online

  24. Pingback: - do si dos strain

  25. Pingback: marlin 1895 sbl for sale

  26. Pingback: cimarron man with no name for sale

  27. Pingback: sig sp2022

  28. Pingback: walther mp5 for sale

  29. Pingback: uca.com.vn

  30. Pingback: DMT AYAHUASCA FOR SALE

  31. Pingback: buy real drivers license online

  32. Pingback: HELIX 1 MG ALPRAZOLAM

  33. Pingback: sbgv245 for sale

  34. Pingback: beretta 92fs inox

  35. Pingback: krt vape cartridges

  36. Pingback: krt cart

  37. Pingback: krt cart

  38. Pingback: car cleaner gbl

  39. Pingback: buy Fentanyl injection online

  40. Pingback: card to cryptocurrency exchange

  41. Pingback: Cannabis Oil

  42. Pingback: cryptocurrency card buy

  43. Valuable info. Lucky me I found your web site by accident, and Im shocked why this accident did not happened earlier! I bookmarked it

  44. Pingback: colt python 2020

  45. Pingback: lottovip

  46. Pingback: walther ppq vs hk vp9

  47. Pingback: Herbal Incense

  48. Hey man, was just looking through the net looking for some info and came across your post. I am impressed by the info that you have on this blogsite. It shows how well you understand this topic. bookmarked this page, will come back for more. You, my friend, ROCK!!!

  49. Lets start absorbing that now.

  50. Pingback: tik tok funny video

Leave a Reply

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