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);
Resources these as the one you mentioned here will be incredibly useful to myself! I will publish a hyperlink to this page on my individual blog. I am positive my site guests will discover that very useful.
Hullo, simply turned into aware of your website thru Yahoo, and found that its really educative. I’m gonna be careful for brussels. I will appreciate when you continue this in future. Lots of other people shall be benefited from your post. Thanks!
Pingback: PlayStation 5 for Sale
Pingback: acpspectrum
Pingback: enhydro quartz for sale
Pingback: atasehir escort
Pingback: spain
Pingback: pc games for windows xp
Pingback: download app apk for windows pc
Pingback: free download for windows 7
Pingback: apps for pc
Pingback: free download for windows 10
Pingback: pc games for windows 8
Pingback: 0xC66D8B9fA986ffA193951ff6E2e122974C42313C
Pingback: torras
Pingback: apps for pc download
Pingback: cz bren 2
Pingback: box sleeves
Pingback: 5.56 green tips
Pingback: Uganda safaris tours
Pingback: Uganda safari tours
Pingback: Krt vape
Pingback: junk cars
Pingback: BERETTA 92FS FOR SALE
Pingback: FLINTLOCK PISTOL FOR SALE
Pingback: Buy Marijuana Online
Pingback: holandalucia
What i dont realize is actually how youre not really much more well-liked than you might be right now. Youre very intelligent. You realize therefore significantly relating to this subject, produced me personally consider it from numerous varied angles. Its like women and men arent fascinated unless it is one thing to accomplish with Lady gaga! Your own stuffs nice. Always maintain it up!
Pingback: cryptocurrency wallet with virtual card
Pingback: mazhor4sezon
Pingback: Bubblegum Haupia Strain
Pingback: filmfilmfilmes
Pingback: Platinum haupia
Pingback: gRh9UPV
Pingback: Haupia strain
Pingback: Bubble hash
Pingback: Moroccan hash
Pingback: Bubble hash
Pingback: 9-05-2022
Pingback: kinoteatrzarya.ru
Pingback: Xvideos
Pingback: XVIDEOSCOM Videos
Pingback: DPTPtNqS
Pingback: qQ8KZZE6
Pingback: D6tuzANh
Pingback: 3NOZC44
Pingback: tor-lyubov-i-grom
Pingback: film-tor-2022
Pingback: hd-tor-2022
Pingback: hdorg2.ru