I am working with Spring Data JPA and I have the following entities: Country: {country_id, country_name} Region: {region_id, region_name, country_id}
There is a onetomany relationship from Country to Region.
I have a JpaRepository for Country and I have a method like the following:
@Query("SELECT new org.example.CountryDTO(a.countryId, a.countryName, b.regionName ) FROM Country AS a left join Region AS lpd on a. countryId = r. countryId WHERE a. countryId = LOWER(:country) and r.regionName= LOWER(:region) ”)
List<CountryDTO> find(@Param("country") String country, @Param("region") String region);
The question is, is there any way to avoid having to specify an SQL query? Ideally I would like to do something like:
List<CountryDTO> findByCountryAndRegionName(@Param("country") String country, @Param("region") String region);
However, that seems a bit complicated because the region_name column comes from the Region table.
Has anybody done anything like that? Any elegant solution that you might propose?
Any help will be greatly appreciated!