One to Many search using AND condition

纵然是瞬间 提交于 2019-12-02 04:04:54

the idea is we select all products with the colors and count each product, then products with both colors should have a count of 2 as the number of colors

DetachedCriteria colorCrit = DetachedCriteria.For(Product.class)
    .createAlias("colors","color")
    .add(Restriction.eq("color.color", "RED")
    .add(Restriction.eq("color.color", "GREEN")
    .SetProjection(Projections.Group("id"))
    .add(Restriction.eq(Projections.rowCount(), 2));

Criteria criteria = createCriteria()
    .add(Subqueries.in("id", colorCrit)
    .list();

Update:

there is an issue for hibernate for exactly this. the last comment describes how to use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!