简体   繁体   English

可以从集合属性中匹配的休眠限制条件

[英]Hibernate Restriction criteria to match from a collection property

I have a entity with a @ElementCollection field using hibernate eg: 我有一个使用冬眠的@ElementCollection字段的实体,例如:

@ElementCollection(fetch = FetchType.EAGER)
@Column(name="years")
private Set<Integer> years = new HashSet<>();

where years is a set of years, I want to only filter those records which contain the particular queryYear in that set of years. 其中years是一组年份,我只想过滤包含该组年份中特定queryYear的那些记录。

What kind of Restriction criteria can I use in Hibernate as Restriction.in doesn't work vice versa 我可以在Hibernate中使用哪种限制条件,因为Restriction.in无效,反之亦然

You cannot do this in one criteria, since this cannot be done also in one normal query without using some syntax like HAVING , or using multiple join or other tricky workaround. 您不能在一个条件中执行此操作,因为如果不使用诸如HAVING语法,或者使用多个联接或其他棘手的解决方法,也无法在一个常规查询中完成此操作。

The closest criteria is using Restrictions.in then using agregate to count the row number, something like this: 最接近的条件是使用Restrictions.in,然后使用agregate来计算行号,如下所示:

c.add(Restrictions.in("years", years));
c.setProjection(Projections.projectionList()
    .add(Projections.groupProperty("id"))
    .add(Projections.count("id")));

All the result that have count = years.size is the id that you want. 所有具有count = years.size的结果就是您想要的ID。

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

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