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. This was a thoroughly insightful enjoy reading. Thank you for sharing your expertise!

  2. Your writing style is captivating! I was engaged from start to finish.

  3. This article was a joy to enjoy reading. Your enthusiasm is contagious!

  4. This article is a perfect blend of informative and entertaining. Well done!

  5. I’m in awe of the way you handle topics with both grace and authority.

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

  7. This piece was beautifully written and incredibly informative. Thank you for sharing!

  8. I’m in awe of the way you handle topics with both grace and authority.

  9. This was a great enjoy reading—thought-provoking and informative. Thank you!

  10. Thank you for adding value to the conversation with your insights.

  11. Your work is truly inspirational. I appreciate the depth you bring to your topics.

  12. Your piece was both informative and thought-provoking. Thanks for the great work!

  13. Your creativity and intelligence shine through this post. Amazing job!

  14. This article was a delightful enjoy reading. Your passion is clearly visible!

  15. Thank you for adding value to the conversation with your insights.

  16. I appreciate the balance and fairness in your writing. Great job!

  17. This article was a delightful enjoy reading. Your passion is clearly visible!

  18. I’m bookmarking this for future reference. Your advice is spot on!

  19. Your insights have added a lot of value to my understanding. Thanks for sharing.

  20. You’ve opened my eyes to new perspectives. Thank you for the enlightenment!

  21. You have a unique perspective that I find incredibly valuable. Thank you for sharing.

  22. Your blog is a constant source of inspiration and knowledge. Thank you!

  23. This blog is a treasure trove of knowledge. Thank you for your contributions!

  24. What a refreshing take on this subject. I completely agree with your points!

  25. You’ve articulated your points with such finesse. Truly a pleasure to enjoy reading.

  26. Your ability to distill complex concepts into enjoy readingable content is admirable.

  27. A masterpiece of writing! You’ve covered all bases with elegance.

  28. You’ve done a fantastic job of breaking down this topic, like unlocking a door to a secret garden. Intrigued to explore more.

  29. The writing style is captivating. Finally, something that can keep my attention longer than a TikTok video.

  30. Shedding light on this subject like you’re the only star in my night sky. The brilliance is refreshing.

  31. This was a thoroughly insightful read. Thank you for sharing The expertise!

  32. You’ve articulated your points with such finesse. Truly a pleasure to enjoy reading.

  33. Your ability to distill complex concepts into enjoy readingable content is admirable.

  34. Your insights have added a lot of value to my understanding. Thanks for sharing.

  35. This post is a testament to your expertise and hard work. Thank you!

  36. You’ve opened my eyes to new perspectives. Thank you for the enlightenment!

  37. This article is a perfect blend of informative and entertaining. Well done!

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

  39. Your attention to detail is remarkable. I appreciate the thoroughness of your post.

  40. Thank you for adding value to the conversation with your insights.

  41. This is one of the most comprehensive articles I’ve enjoy reading on this topic. Kudos!

  42. Please let me know if you’re looking for a author for your blog. You have some really great articles and I feel I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some content for your blog in exchange for a link back to mine. Please shoot me an e-mail if interested. Thanks!

  43. A beacon of knowledge, or so I thought until I realized it’s just The shining confidence.

  44. I’m impressed by The ability to convey such nuanced ideas with clarity.

  45. Truly inspirational work, or so I tell myself as I avoid my own projects.

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

  47. Remember, the key with flirtatious comments is to keep them light-hearted, respectful, and ensure they’re taken in the spirit of fun and admiration.

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

  49. I admire the way you tackled this hard to understand issue. Very enlightening!

  50. The article was a delightful read. It’s clear you’re passionate about what you do, and it shows.

Leave a Reply

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