topcan5
topcan5

Reputation: 1707

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;

Upvotes: 2

Views: 11929

Answers (1)

Manish Maheshwari
Manish Maheshwari

Reputation: 4134

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.

Upvotes: 4

Related Questions