2015-02-17 140 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