2015-02-17 202 views
2

我有一个LOCATION实体,它包含COUNTRY,STATE和CITY。我有一个LocationRepository接口定义为:Spring Data JPA:查找嵌套对象

public interface SpringDataLocationRepository extends LocationRepository, 
     Repository<Location, Integer> { 
} 

我想查找所有国家的国家。我可以按照方法名称标准查询LOCATION实体的所有内容。如果我想List,是否需要创建StateRepository接口并在那里查询关于STATE的所有内容?如果我可以从LocationRepository中获取它,那么这个方法是什么样的?我认为它会看起来像下面(当然不起作用)。

List findStateByCountryCountryId(Integer id)throws DataAccessException;

回答

3

下面的方法应该工作:

List<Location> findByCountryId(Integer id) 

然后,你可以从列表中Location对象得到国家。

PS:当然,这不是我测试/执行的。

+1

这个相关问题/回答比较透彻地解释了这种情况 - http://stackoverflow.com/questions/24441411/spring-data-jpa-find-by-embedded-object-property – chrismarx 2015-04-28 19:35:18