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 PlantSchedule
s 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);
This post is a testament not only to The expertise but also to The dedication. Truly inspiring.
I admire the way you tackled this hard to understand issue. Very enlightening!
A refreshing take on the subject, like a cool breeze on a hot day. I’m all ears for what you have to say next.
Thanks for the hard work. I could almost see the sweat on the keyboard. Much appreciated!
The information you’ve shared has been a revelation for me. Incredibly enlightening!
Each post is a journey, and The words are the map. Thanks for leading the way.
I’m so grateful for the information you’ve shared. It’s been incredibly enlightening!
I always learn something new from The posts, like discovering new facets of a gem. Thanks for the gems!
Reading The Writing is like finding an oasis in a desert of information. Refreshing and revitalizing.
The elegance of The arguments is as captivating as a sunset. I could admire it all day.
The Writing is a constant source of inspiration and knowledge. Thank you!
A constant source of inspiration and knowledge, like a muse but less mythical.
The insights added a lot of value, in a way only Google Scholar dreams of. Thanks for the enlightenment.
This is one of the most comprehensive articles I’ve read on this topic. Kudos!
The research depth is so evident, I almost thought this was a thesis defense.
This was a thoroughly insightful read. Thank you for sharing The expertise!
The passion for this subject is infectious. Reading The post has inspired me to learn more.
Thank you for adding value to the conversation with The insights.
The Writing is like a secret garden, each post a path leading to new discoveries and delights.
A constant source of inspiration and knowledge, like a muse but less mythical.
A refreshing take on the subject, like a cool breeze on a hot day. I’m all ears for what you have to say next.
This post has been incredibly helpful, like a guiding hand in a crowded room. The guidance is much appreciated.
Bookmarking this! The practical advice is something I’ll definitely be coming back to.
The Writing is a go-to resource, like a favorite coffee shop where the barista knows The order. Always comforting.
I appreciate the unique viewpoints you bring to The writing. Very insightful!
The Writing is like a gallery of thoughts, each post a masterpiece worthy of contemplation.
I’m so grateful for the information you’ve shared. It’s been incredibly enlightening!
Opened my eyes to new perspectives, and here I was thinking I’d seen it all.
I’m impressed by The ability to convey such nuanced ideas with clarity.
Reading The work is like gazing at a masterpiece; every detail contributes to a breathtaking whole.
Reading this gave me a lot of insights. The expertise really shines through, and I’m grateful for it.
Reading The work is like catching up with an old friend; comfortable, enlightening, and always welcome.
Truly inspirational work, or so I tell myself as I avoid my own projects.
The article was a delightful read, and The passion shines as brightly as The intellect. Quite the combination!
You’ve articulated The points with such finesse. Truly a pleasure to read.
I’m officially a fan of The work. It’s like having a crush, but intellectually stimulating.
I appreciate the unique viewpoints you bring to The writing. Very insightful!
The posts inspire me regularly. The depth you bring to The topics is truly exceptional.
This piece was beautifully written and incredibly informative. Thank you for sharing!
The work is truly inspirational. I appreciate the depth you bring to The topics.
The piece was both informative and thought-provoking. Thanks for the great work!
The Writing is like a trusted compass, always pointing me in the direction of enlightenment.
This post is packed with useful insights. Thanks for sharing The knowledge!
Reading this gave me a lot of insights. The expertise really shines through, and I’m grateful for it.
Reading The post was like going on a first date with my mind. Excited for the next rendezvous.
The ability to convey hard to understand ideas so effortlessly is as attractive as a perfectly tailored suit.
Every word you write sparkles with insight, like stars in my night sky. Can’t wait to navigate more skies together.
Always excited to see The posts, like waiting for a message from a crush. Another excellent read!
The ability to convey nuanced ideas with clarity is as alluring as a whispered secret.
Every article you write is like a new adventure. I’m always excited to see where you’ll take me next.