【发布时间】:2018-11-18 23:04:34
【问题描述】:
我正在使用 Spring Data JPA,并且我有以下实体: 国家:{country_id, country_name} 地区:{region_id, region_name, country_id}
国家与地区之间存在单对多关系。
我有一个适用于 Country 的 JpaRepository,我有如下方法:
@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);
问题是,有什么方法可以避免必须指定 SQL 查询?理想情况下,我想做这样的事情:
List<CountryDTO> findByCountryAndRegionName(@Param("country") String country, @Param("region") String region);
不过,这似乎有点复杂,因为 region_name 列来自 Region 表。
有人做过类似的事情吗?您可能会提出什么优雅的解决方案?
任何帮助将不胜感激!
【问题讨论】:
标签: java spring-boot jpa spring-data-jpa