繁体   English   中英

hql in-clause 在子集合中有多次点击

[英]hql in-clause with multiple hits in child collection

我有一个名为Recipe的表/实体,其子集合类型为Tag 我希望能够找到通过两个或多个标签搜索的食谱。 就像是:

SELECT re FROM Recipe re JOIN re.tags t WHERE t in :tagsIds

但我只想要那些标签集合包含所有 tagId 的命中。 在 HQL/SQL 中可能吗? (也许使用标准?)

提前致谢。

我假设您有两个不同的实体 Recipe 和 Tag,可以按如下方式完成。

    Criteria criteria = getSession().createCriteria("Recipe.class");
    criteria.createAlias("tags", "tag");
    criteria.add(Restrictions.in("tag.id", Arrays.asList(1,2,3)));
    return (List<Recipe>) criteria.list();

我相信您可能在 HQL 中缺少括号。 它应该是:

... WHERE t in (:tagsIds)

好的,所以它做到了。 感谢您的回复。

String hql = "select r from Recipe r " +
                "join r.tags t " +
                "where t.id in (:tags) " +
                "group by r " +
                "having count(t)=:tag_count";
Query query = session.createQuery(hql);
query.setParameterList("tags", tagIds);
query.setInteger("tag_count", tagIds.size());
List<Recipe> recipes = query.list();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM