【问题标题】:Spring findBy query method to find enabled countries together with the country containing a certain cityspring findBy 查询方法查找启用的国家以及包含某个城市的国家
【发布时间】:2016-05-31 19:18:44
【问题描述】:

我有 CountryCity 的课程:

Country 类具有以下属性:

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

private boolean enabled;

现在我需要找到所有启用的国家以及包含某个城市的国家:

不是这个:

findDistinctByEnabledOrCitiesNotNull(boolean enabled)

因为在这种情况下,我会找到启用的国家/地区以及包含城市的所有禁用的国家/地区。

我需要类似的东西:

findByEnabledOrCitiesContaining(boolean enabled, City city)

有可能吗?


SQL 应该是这样的:

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

【问题讨论】:

    标签: java spring jpa spring-data spring-repositories


    【解决方案1】:

    对不起。我的方法findByEnabledOrCitiesContaining(boolean enabled, City city) 就是解决方案。它只是因为其他原因而不起作用。

    所以,这个问题不需要更多的答案,可能会被关闭。

    谢谢!

    【讨论】:

    • 我知道我可以删除。如果其他人有同样的问题,我只是让它与答案保持一致。如果版主认为它可能不会留下来,那是另一个问题。我无法将自己的答案标记为已接受。
    • 您可以将您的答案标记为已接受,在此之前您只需have to wait 48 hours
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2016-12-28
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多