简体   繁体   中英

Spring Data JPA: find nested object

I have a LOCATION entity and it contains COUNTRY, STATE and CITY. And I have a LocationRepository interface defined as:

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

I want to find all the state by country. I can follow the method name standard to query everything for LOCATION entity. If I want List, do I need to create StateRepository interface and query everything about STATE in there? If I can just get it from LocationRepository, what's the method looks like? I assume it will look something like below (of course it doesn't work).

List findStateByCountryCountryId(Integer id) throws DataAccessException;

Below method should work:

List<Location> findByCountryId(Integer id)

Then you can get state from the Location objects in the list.

PS: Of course, this is not tested/executed by me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM