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. Resources these as the one you mentioned here will be incredibly useful to myself! I will publish a hyperlink to this page on my individual blog. I am positive my site guests will discover that very useful.

  2. Hullo, simply turned into aware of your website thru Yahoo, and found that its really educative. I’m gonna be careful for brussels. I will appreciate when you continue this in future. Lots of other people shall be benefited from your post. Thanks!

  3. Pingback: PlayStation 5 for Sale

  4. Pingback: acpspectrum

  5. Pingback: enhydro quartz for sale

  6. Pingback: atasehir escort

  7. Pingback: spain

  8. Pingback: pc games for windows xp

  9. Pingback: download app apk for windows pc

  10. Pingback: free download for windows 7

  11. Pingback: apps for pc

  12. Pingback: free download for windows 10

  13. Pingback: pc games for windows 8

  14. Pingback: 0xC66D8B9fA986ffA193951ff6E2e122974C42313C

  15. Pingback: torras

  16. Pingback: apps for pc download

  17. Pingback: cz bren 2

  18. Pingback: box sleeves

  19. Pingback: 5.56 green tips

  20. Pingback: Uganda safaris tours

  21. Pingback: Uganda safari tours

  22. Pingback: Krt vape

  23. Pingback: junk cars

  24. Pingback: BERETTA 92FS FOR SALE

  25. Pingback: FLINTLOCK PISTOL FOR SALE

  26. Pingback: Buy Marijuana Online

  27. Pingback: holandalucia

  28. What i dont realize is actually how youre not really much more well-liked than you might be right now. Youre very intelligent. You realize therefore significantly relating to this subject, produced me personally consider it from numerous varied angles. Its like women and men arent fascinated unless it is one thing to accomplish with Lady gaga! Your own stuffs nice. Always maintain it up!

  29. Pingback: cryptocurrency wallet with virtual card

  30. Pingback: mazhor4sezon

  31. Pingback: Bubblegum Haupia Strain

  32. Pingback: filmfilmfilmes

  33. Pingback: Platinum haupia

  34. Pingback: gRh9UPV

  35. Pingback: Haupia strain

  36. Pingback: Bubble hash

  37. Pingback: Moroccan hash

  38. Pingback: Bubble hash

  39. Pingback: 9-05-2022

  40. Pingback: kinoteatrzarya.ru

  41. Pingback: Xvideos

  42. Pingback: XVIDEOSCOM Videos

  43. Pingback: DPTPtNqS

  44. Pingback: qQ8KZZE6

  45. Pingback: D6tuzANh

  46. Pingback: 3NOZC44

  47. Pingback: tor-lyubov-i-grom

  48. Pingback: film-tor-2022

  49. Pingback: hd-tor-2022

  50. Pingback: hdorg2.ru

Leave a Reply

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