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);
Reading The Writing is like finding an oasis in a desert of information. Refreshing and revitalizing.
The ability to break down tough concepts is as impressive as a magician’s trick. Color me amazed.
The work is both informative and thought-provoking. I’m really impressed by the high quality of The content.
The clarity and thoughtfulness of The approach is as appealing as a deep conversation over coffee.
The grace and authority with which you handle topics are awe-inspiring. I always come away with new knowledge.
A masterpiece of writing! You’ve covered all bases with elegance.
The depth of The research really stands out. It’s clear you’ve put a lot of thought into this.
I appreciate the balance and fairness in The writing. Great job!
The insights are like a favorite book; I find new treasures each time I return.
I’m so glad I stumbled upon this article. It was exactly what I needed to read!
The elegance of The prose is like a fine dance, each word stepping gracefully to the next.
Fantastic job breaking down this topic, like a demolition crew for my misconceptions.
I admire the way you tackled this hard to understand issue. Very enlightening!
The commitment to high quality content really shows. I’m always excited to read The work.
This piece was beautifully written and incredibly informative. Thank you for sharing!
A breath of fresh air, or what I needed after being suffocated by mediocrity.
The clarity and thoughtfulness of The approach is as appealing as a deep conversation over coffee.
The attention to detail is as attractive as it is thorough. I appreciate a person who notices the little things.
The words are like brush strokes on a canvas, painting ideas in my mind.
The insights are like keys, unlocking new perspectives and ideas I hadn’t considered.
The Writing has become like a favorite meeting spot, where great minds and ideas mingle.
The insights add so much value, like an unexpected compliment that brightens one’s day. Thanks for sharing.
The insights add so much value to the conversation. I always learn something new from you.
The Writing is a constant source of inspiration and knowledge for me. I can’t thank you enough.
The Writing is a constant source of inspiration and knowledge for me. I can’t thank you enough.
Reading The work is like watching the sunrise, a daily reminder of beauty and new beginnings.
Each post you share is like a gift, wrapped in the finest paper of eloquence and insight.
The clarity and thoughtfulness of The approach is as appealing as a deep conversation over coffee.
Stumbling upon this article was the highlight of my day, much like catching a glimpse of a smile across the room.
The unique viewpoints in The writing never fail to impress me. Insightful as always!
Grateful for the enlightenment, like I’ve just been initiated into a secret society.
The insights dazzled me more than a candlelit dinner. Thanks for lighting up my intellect.
Every piece you write is like adding another book to my mental library. Thanks for expanding my collection.
Truly inspirational work, or so I tell myself as I avoid my own projects.
The Writing is a constant source of inspiration and knowledge for me. I can’t thank you enough.
You’ve done a fantastic job of breaking down this topic, like unlocking a door to a secret garden. Intrigued to explore more.
The article was a joy to read, and The enthusiasm is as infectious as The charm.
You’ve opened my eyes to new perspectives. Thank you for the enlightenment!
This post is a testament not only to The expertise but also to The dedication. Truly inspiring.
Thank you for consistently producing such high-high quality content.
The writing has the warmth and familiarity of a favorite sweater, providing comfort and insight in equal measure.
The insights are like a sunrise, bringing light and warmth to new ideas.
Reading The article was a joy. The enthusiasm for the topic is really motivating.
Incredibly informative post! I learned a lot and look forward to more.
The insights dazzled me more than a candlelit dinner. Thanks for lighting up my intellect.
This is one of the most comprehensive articles I’ve read on this topic. Kudos!
The Writing is like a warm fireplace on a cold day, inviting me to settle in and stay awhile.
The ability to connect with readers is like a secret handshake, making us feel part of an exclusive club.
The hard work you put into this post is as admirable as The commitment to high quality. It’s very attractive.
The blend of informative and entertaining content is perfect. I enjoyed every word.