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);
Pingback: history-of-ukraine.ru news ukraine
Pingback: newsukraine.ru
Pingback: edu-design.ru
Pingback: tftl.ru
Pingback: brutv
Pingback: site 2023
Pingback: sitestats01
Pingback: 1c789.ru
Pingback: cttdu.ru
Pingback: hdserial2023.ru
Pingback: serialhd2023.ru
Pingback: matchonline2022.ru
Pingback: bit.ly/3OEzOZR
Pingback: bit.ly/3gGFqGq
Pingback: bit.ly/3ARFdXA
Pingback: bit.ly/3ig2UT5
Pingback: bit.ly/3GQNK0J
Pingback: bep5w0Df
Pingback: www
Pingback: icf
Pingback: 24hours-news
Pingback: rusnewsweek
Pingback: uluro-ado
Pingback: irannews.ru
Pingback: klondayk2022
Pingback: tqmFEB3B
Pingback: mangalib
Pingback: x
Pingback: 9xflix
Pingback: xnxx
Pingback: 123movies
Pingback: kinokrad
Pingback: batmanapollo
Pingback: batmanapollo.ru - psychologist
Pingback: batmanapollo psychologist
Pingback: elizavetaboyarskaya.ru
Pingback: vsovezdeisrazu
Pingback: 2023
Pingback: ipsychologos
Pingback: yug-grib.ru
Pingback: studio-tatuage.ru
Pingback: bit.ly/pamfir-pamfir-2023-ua-pamfir
Pingback: poip-nsk.ru - Movie Watch
Thanks for the valuable information
Pingback: film.poip-nsk.ru - film online
Pingback: video.vipspark.ru
Pingback: vitaliy-abdulov.ru
Pingback: psychophysics.ru
Pingback: vipspark.vipspark.ru