Spring Data JPA: find nested object
10,589
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.
Author by
topcan5
Updated on June 04, 2022Comments
-
topcan5 almost 2 years
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;
-
chrismarx about 9 yearsthis related question/answer more thoroughly explains this situation - stackoverflow.com/questions/24441411/…