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);
Pretty nice post. I just stumbled upon your blog and wished to say that I’ve truly enjoyed surfing around your blog posts. After all I will be subscribing to your rss feed and I hope you write again soon!
Thank you for always being there for me.
I have read so many articles or reviews on the topic
of the blogger lovers except this article is in fact a pleasant post,
keep it up.
YfAPqxRtneayTVO
sXryzgIdhnQfCKv
There may be noticeably a bundle to know about this. I assume you made sure good factors in features also.
VGuOnMBrSpcfTNQ
Fantastic web site. Plenty of useful info here. I am sending it to a few pals ans also sharing in delicious. And naturally, thank you in your effort!
Boulder News
But wanna input that you have a very nice web site, I love the style it actually stands out.
Thanks for your personal marvelous posting! I quite enjoyed reading
it, you will be a great author. I will make sure to bookmark your blog and will
come back down the road. I want to encourage that you continue your great job,
have a nice day!
I blog frequently and I really thank you for your content.
The article has really peaked my interest. I will bookmark your site and keep checking for
new details about once a week. I opted in for your Feed as well.
I always emailed this web site post page to all my friends,
since if like to read it then my friends will too.
Thanks for the complete information. You helped me.
Hi just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different browsers and both show the same results.
Your post was a beacon of knowledge. Thank you for illuminating this subject.
You’ve done a fantastic job of breaking down this topic. Thanks for the clarity!
Fantastic site. A lot of helpful info here. I’m sending it to a few pals ans additionally sharing in delicious.
And naturally, thank you to your effort!
Consistently high-quality content, as if you’re trying to show us all up.
There is noticeably a bundle to know about this. I assume you made certain nice points in features also.
Hi my loved one! I wish to say that this article is awesome, nice written and come with almost all vital infos. I’d like to see extra posts like this .
I think other site proprietors should take this site as an model, very clean and magnificent user friendly style and design, let alone the content. You are an expert in this topic!
Your insights are like a fine wine—rich, fulfilling, and leaving me wanting more.
The clarity of your writing is like a perfectly tuned instrument, making complex melodies seem effortless.
As a Newbie, I am always searching online for articles that can benefit me. Thank you
This post was a breath of fresh air. Thank you for your unique insights!
Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!
Every article you write is like a new adventure. I’m always excited to see where you’ll take me next.
I appreciate how you’ve explained things so clearly. It really helped me understand the topic better.
You have a unique perspective that I find incredibly valuable. Thank you for sharing.
Your blog is a go-to resource for me. Thanks for all the hard work!
Your passion for this subject shines through your words. Inspiring!
You’ve done a fantastic job of breaking down this topic. Thanks for the clarity!
This is a brilliant piece of writing. You’ve nailed it perfectly!
This piece was beautifully written and incredibly informative. Thank you for sharing!
Incredibly informative post! I learned a lot and look forward to more.
Your ability to distill complex concepts into enjoy readingable content is admirable.
I’m amazed by the depth and b enjoy readingth of your knowledge. Thanks for sharing!
I’m always excited to see your posts in my feed. Another excellent article!
I’m amazed by the depth and b enjoy readingth of your knowledge. Thanks for sharing!
I admire the way you tackled this complex issue. Very enlightening!
Your post resonated with me on many levels. Thank you for writing it!
You’ve opened my eyes to new perspectives. Thank you for the enlightenment!
This is a brilliant piece of writing. You’ve nailed it perfectly!
I’m so glad I stumbled upon this article. It was exactly what I needed to enjoy reading!
Thank you for shedding light on this subject. Your perspective is refreshing!
I appreciate the balance and fairness in your writing. Great job!
Your insights have added a lot of value to my understanding. Thanks for sharing.
You’ve done a fantastic job of breaking down this topic. Thanks for the clarity!
This article was a joy to enjoy reading. Your enthusiasm is contagious!