Search code examples
javaspringjpaspring-dataspring-repositories

Spring findBy query method to find enabled countries together with the country containing a certain city


I have classes Country and City:

The Country class has the properties:

@OneToMany(mappedBy = "country")
private Set<City> cities;

private boolean enabled;

Now I need to find all enabled countries together with the country containing a certain city:

Not this one:

findDistinctByEnabledOrCitiesNotNull(boolean enabled)

because in this case I find enabled countries together with all disabled countries containing cities.

I need something like:

findByEnabledOrCitiesContaining(boolean enabled, City city)

Is it possible?


SQL would be something like this:

select country.id, country.name, country.enabled from country
left join city on city.countryid = country.id
where country.enabled = 1 or city.id = 1

Solution

  • I'm sorry. My method findByEnabledOrCitiesContaining(boolean enabled, City city) is the solution. It just didn't work because of something else.

    So, this question needs no more answers and may be closed.

    Thank you!