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);
best buy hacliknk instagram goread services.
WOW just what I wass searchimg for. Came hhere bby
searching foor 80416
Petty sectionn of content. I just stumbled upon your wweb ste and in accession caputal to assert that I aacquire actually enjoyed account your log posts.
Any wayy I’ll bee subscribing tto your augment andd egen I achievement
yyou acccess consistently fast.
Hi there i amm kavin, itss my first time too commentting anyplace, when i reawd tbis paragraph i thought i coupd alzo crreate cojment due
too thyis brilliant post.
The grace and authority you handle topics with are as mesmerizing as a moonlit dance. I’m thoroughly impressed.
Distilling hard to understand concepts into readable content, or what I like to call, a miracle.
The insights added a lot of value, in a way only Google Scholar dreams of. Thanks for the enlightenment.
The voice shines through The writing like a beacon, guiding us through the darkness of ignorance.
Such a well-researched piece! It’s evident how much effort you’ve put in.
I appreciate the unique viewpoints you bring to The writing. Very insightful!
I look forward to The posts because they always offer something valuable. Another great read!
The breadth of The knowledge is amazing. Thanks for sharing The insights with us.
Helpful info. Fortunate me I found your site by chance, and I’m
stunned why this accident did not took place in advance! I bookmarked it.
I go to see everyday a few web sites and blogs
to read content, except this web site provides quality based articles.
It’s hard to find experienced people about this subject, but you seem
like you know what you’re talking about! Thanks
Since the admin of this web site is working, no doubt very soon it
will be renowned, due to its quality contents.
Always excited to see The posts, like waiting for a message from a crush. Another excellent read!
The work is truly inspirational. I appreciate the depth you bring to The topics.
The depth of The understanding is as mesmerizing as the ocean. I’m ready to dive in.
Always excited to see The posts, like waiting for a message from a crush. Another excellent read!
The insights are like a fine wine—rich, fulfilling, and leaving me wanting more.
The post has been incredibly helpful. Thank you for the guidance!
The writing is a constant source of inspiration and knowledge for me on the state of the country. Thank you!
Bookmarking this! The practical advice on the state of the country is something I’ll definitely be coming back to.
The creativity and intelligence shine through, blinding almost, but I’ll keep my sunglasses handy.
The insights light up my intellect like fireworks. Thanks for the show!
A masterpiece of writing! You’ve covered all bases with elegance.
I learned a lot, and now I’m curious about what else you could teach me. The intelligence is as captivating as The prose.
Consistently high-high quality content, as if you’re trying to show us all up.
You’ve opened my eyes to new perspectives, as if you knew the way to my curious heart.
This is the most thorough piece I’ve read on the topic. The dedication to research is admirable.
This article is a perfect blend of informative and entertaining content on the state of the country. Well done!
The attention to detail didn’t go unnoticed. I really appreciate the thoroughness of The approach.
The Writing is a go-to resource for me. Thanks for all the hard work!
The thought-provoking post has me looking forward to more. It’s like the intellectual equivalent of a second date.
The passion you pour into The posts is like a flame, igniting curiosity and warming the soul.