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);
Hi, i think that i saw you visited my blog so i came to “return the favor”.I am attempting to find things to improve my site!I suppose its ok to use a few of your ideas!!
Pingback: Google
Pingback: free download for windows 10
Pingback: free app for pc download
Pingback: pc games for windows 10
Pingback: download apk app for pc
Pingback: Monkeys For Sale
Pingback: Agence de communication
Pingback: IT Services Somerset
Pingback: Sig Sauer Handguns
Pingback: Pinball Machines for Sale
Pingback: Shipping containers For Sale
Pingback: Macbook pro akku schnell leer meilen
Pingback: FARMING EQUIPMENT FOR SALE IN ZIMBABWE
Pingback: shipping containers for sale
Pingback: aromatherapy shop
Pingback: website
Pingback: Buy Methadone 10mg online
Pingback: GELATI STRAIN
Pingback: mossberg 590 18.5
Pingback: savage impulse rifle for sale
Pingback: zip 22 for sale
Pingback: Liquid pentobarbital for sale
Pingback: mossberg 930 combo for sale
Pingback: stiiizy battery alternative Archives
Pingback: magic mushrooms
Pingback: buy skittles exotic carts online
Pingback: background check
Pingback: beretta semi auto shotgun
Pingback: canadian marble fox
Pingback: smith and wesson arms
Pingback: catnapper recliners
Pingback: weed for sale in uk
Pingback: 3080 ftw3 msrp
Pingback: best dumbells
Pingback: شركة تسويق
Pingback: Ruger AR-556 Semi-Auto Rifle
Pingback: G17 GEN5 MOS FS STANDARD | 9X19MM
Pingback: RissMiner
Pingback: Sig Sauer Official Gun Shop
Pingback: vertigo
Pingback: rhonda worthey
Pingback: savage grow plus scam
Pingback: Safari Holidays in Kenya
Pingback: beretta guns for sale
Pingback: buy raw garden carts online
Pingback: sig sauer rifles
Pingback: kopru altinda sikin benim
Pingback: springfield saint edge for sale
Pingback: ufo extracts cartridges