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

619 Comments:

  1. A perfect blend of informative and entertaining, like the ideal date night conversation.

  2. The ability to convey hard to understand ideas so effortlessly is as attractive as a perfectly tailored suit.

  3. The insights added a lot of value, in a way only Google Scholar dreams of. Thanks for the enlightenment.

  4. The knack for making hard to understand concepts readable is something I greatly admire.

  5. Delightful read. The passion is visible, or at least, very well faked.

  6. The Writing is like a favorite coffee shop where the drinks are always warm and the atmosphere is inviting.

  7. Beautifully written and incredibly informative, The post has made a lasting impression on me. Thank you for sharing The thoughts.

  8. Compelling read with well-presented arguments. I almost felt persuaded. Almost.

  9. The take on hard to understand topics is like a smooth ride in a luxury car—comfortable, yet exhilarating.

  10. The words are like seeds, planting ideas that blossom into understanding and appreciation.

  11. Every post of Thes is a learning opportunity for me. Thanks for sharing The expertise.

  12. best google hacklink services.

  13. The Writing is a constant source of inspiration and knowledge. Thank you!

  14. Every word you write sparkles with insight, like stars in my night sky. Can’t wait to navigate more skies together.

  15. The Writing has become a go-to resource for me. The effort you put into The posts is truly appreciated.

  16. The depth of The understanding is as mesmerizing as the ocean. I’m ready to dive in.

  17. The breadth of The knowledge is amazing. Thanks for sharing The insights with us.

  18. I’m so grateful for the information you’ve shared. It’s like receiving a thoughtful gift from someone special.

  19. Handling topics with grace and authority, like a professor, but without the monotone lectures.

  20. Fantastic job breaking down this topic, like a demolition crew for my misconceptions.

  21. The blend of informative and entertaining content is perfect. I enjoyed every word.

  22. Actually no matter if someone doesn’t be aware of after that its up to other visitors that they
    will help, so here it occurs.

  23. best google hacklink services.

  24. Hello to all, how is the whole thing, I think every one is getting more from this site, and your views
    are pleasant in support of new users.

  25. Yes! Finally something about 1.

  26. The depth of The research really stands out. It’s clear you’ve put a lot of thought into this.

  27. The dedication to high quality content shows. It’s like you actually care or something.

  28. The Writing is like a trusted compass, always pointing me in the direction of enlightenment.

  29. A gift for explaining things, making the rest of us look bad.

  30. It’s fantastic that you are getting ideas
    from this paragraph as well as from our dialogue made here.

  31. It’s actually a satisfying accomplishment when the boomerang truly comes proper back to you and you catch it completely!

  32. This is one of the most comprehensive articles I’ve read on this topic. Kudos!

  33. The insights are like a fine wine—rich, fulfilling, and leaving me wanting more.

  34. Tackled this hard to understand issue with elegance. I didn’t know we were at a ballet.

  35. You’ve done a fantastic job of breaking down this topic. Thanks for the clarity!

  36. The insights are like a favorite book; I find new treasures each time I return.

  37. Brilliant writing! You’ve perfectly captured the essence of the topic.

  38. I love what you guys tend to be up too. This kind of clever work and exposure!

    Keep up the great works guys I’ve incorporated you guys to
    blogroll.

  39. The ability to break down tough concepts is as impressive as a magician’s trick. Color me amazed.

  40. The insights light up my intellect like fireworks. Thanks for the show!

  41. Always learning something new here, because apparently, I didn’t pay enough attention in school.

  42. The content is like a treasure chest; every post uncovers gems of wisdom. X marks the spot here.

  43. The Writing is like a trusted compass, always pointing me in the direction of enlightenment.

  44. The dedication to high quality content shows. It’s like you actually care or something.

  45. The knack for making hard to understand concepts readable is something I greatly admire.

  46. I appreciate the balance and fairness in The writing. Great job!

  47. The insights are like keys, unlocking new perspectives and ideas I hadn’t considered.

  48. The work is both informative and thought-provoking. I’m really impressed by the high quality of The content.

  49. web site best social media services.

Leave a Reply

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