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

618 Comments:

  1. The post resonated with me on many levels, much like a perfectly tuned love song. Thanks for the harmony.

  2. You’ve articulated The points with such finesse. Truly a pleasure to read.

  3. Thank you for adding value to the conversation with The insights.

  4. Each post is a journey, and The words are the map. Thanks for leading the way.

  5. The post has been incredibly helpful. Thank you for the guidance!

  6. What a refreshing take on this subject. I completely agree with The points!

  7. Thoroughly insightful read, or so I thought until I realized it was The expertise shining through. Thanks for making me feel like a novice again!

  8. I appreciate the balance and fairness in The writing, like a perfect partner who always keeps things interesting. Great job!

  9. Opened my eyes to new perspectives, and here I was thinking I’d seen it all.

  10. The grace and authority you handle topics with are as mesmerizing as a moonlit dance. I’m thoroughly impressed.

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

  12. Each post you write is like a letter I’ve been waiting for. Always delivered with care.

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

  14. The insights have added a lot of value to my understanding. Thanks for sharing.

  15. The posts are like a secret garden of knowledge. I’m always excited to see what’s blooming.

  16. This post was a breath of fresh air. Thank you for The unique insights!

  17. I’m so grateful for the information you’ve shared. It’s been incredibly enlightening!

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

  19. The piece was both informative and thought-provoking, like a deep conversation that lingers into the night.

  20. The words are like brush strokes on a canvas, painting ideas in my mind.

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

  22. The fresh insights were a breath of fresh air. Thank you for sharing The unique perspective.

  23. Breaking down this topic so clearly was no small feat. Thanks for making it accessible.

  24. The writing style had me at hello. Engaged from start to finish, just like a perfect first date.

  25. This post has been incredibly helpful to me. The guidance is something I’m truly grateful for.

  26. Testament to The expertise and hard work, or The ability to make me feel utterly unaccomplished.

  27. The elegance of The arguments is as captivating as a sunset. I could admire it all day.

  28. This post is packed with insights I hadn’t considered before. Thanks for broadening my horizons.

  29. The insights are like a sunrise, bringing light and warmth to new ideas.

  30. The passion you pour into The posts is like a flame, igniting curiosity and warming the soul.

  31. This article was a delightful read. The passion is clearly visible!

  32. Thanks for the hard work. I could almost see the sweat on the keyboard. Much appreciated!

  33. The piece was both informative and thought-provoking, like a deep conversation that lingers into the night.

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

  35. Reading The work is like watching the sunrise, a daily reminder of beauty and new beginnings.

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

  37. The Writing is a go-to resource, like a favorite coffee shop where the barista knows The order. Always comforting.

  38. I do like the way you have framed this specific concern plus it really does supply me some fodder for thought. Nonetheless, from what I have personally seen, I just simply wish as other comments pack on that people stay on point and in no way start on a tirade of some other news du jour. Yet, thank you for this exceptional piece and while I can not go along with the idea in totality, I regard your viewpoint.

  39. I’d have to examine with you here. Which is not one thing I usually do! I take pleasure in reading a post that may make folks think. Additionally, thanks for permitting me to comment!

  40. WOW just what I was looking for. Came here by searching for wikipedia

  41. The attention to detail is as attractive as it is thorough. I appreciate a person who notices the little things.

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

  43. The research depth is so evident, I almost thought this was a thesis defense.

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

  45. The writing style had me at hello. Engaged from start to finish, just like a perfect first date.

  46. The dedication to high quality content is evident and incredibly appealing. It’s hard not to admire someone who cares so much.

  47. Provoked thought and taught me something new, as if my brain needed more exercise.

  48. Discovering The Writing has been a game-changer for me. The contributions are invaluable.

  49. Thanks for finally talking about > Spring Data Pageable JPA Filter Query with 3 of 4 Parts of Composite Key via a Query Method – kevinmichaelcoy.com < Loved it!

Leave a Reply

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