简体   繁体   English

休眠条件查询以匹配所有子集合元素

[英]Hibernate criteria query to match against all child collection elements

This question is very similar to this one but the responses were minimal to that question. 这个问题很相似, 这一个 ,但反应是最小的那个问题。

I have a parent class with a Set of child entities. 我有一个带有一组子实体的父类。 The child entities are just a wrapper for a string and live in a different table to the parent entity. 子实体只是字符串的包装器,与父实体位于不同的表中。 I want to have a criteria query that returns the parent entities when all the members of the set of child entities return true to a condition. 我希望有一个条件查询,当子实体集的所有成员都返回true时返回一个父实体。 This condition is matching against one of a list of strings. 此条件与字符串列表之一匹配。 Here's where I am: 这是我的位置:

Criteria c = criteria();
Criteria ands = c.createCriteria("ands");
Disjunction dis = Restrictions.disjunction();
for (String value : values) {
    dis.add(Restrictions.like("value", "%" + value + "%"));
}
ands.add(dis);
return list(c);

"ands" is the set of entities with a "value" field that is a string. “和”是具有“值”字段(是字符串)的一组实体。 "criteria()" creates a criteria for the parent class. “ criteria()”为父类创建一个条件。 "list()" just calls criteria.list(); “ list()”仅调用criteria.list();

This is just matching against any of the elements, rather than all. 这只是与任何元素匹配,而不是全部。

Hope this makes sense. 希望这是有道理的。 Any help much appreciated. 任何帮助,不胜感激。

As a theoretical exercise, you can do something like this: 作为理论练习,您可以执行以下操作:

Criterion condition = ...;

Criteria c = s.createCriteria(Parent.class, "p");
DetachedCriteria dc = DetachedCriteria.forClass(Parent.class, "p2")
    .createCriteria("ands", "c")
    .add(Restrictions.not(condition))
    .add(Property.forName("p.id").eqProperty("p2.id"))
    .setProjection(Projections.id());

c.add(Subqueries.notExists(dc));

However, this approach is not good for practical use because it requires extra join (due to absence of in elements clause in Criteria API). 但是,此方法不适合实际使用,因为它需要额外的join (由于Criteria API中缺少in elements子句)。 Also note that it uses double negation ( SELECT ... WHERE NOT EXISTS (SELECT ... WHERE NOT <condition>) ), so it can have problems with NULL s. 还要注意,它使用双重否定( SELECT ... WHERE NOT EXISTS (SELECT ... WHERE NOT <condition>) ),因此它可能会出现NULL问题。

EDIT: In HQL it can be written like this: 编辑:在HQL中可以这样写:

from Parent p
where not exists (select c from p.ands c where not <condition>)

or 要么

from Parent p
where not exists (select c from Child c 
    where not <condition> and c in elements(p.ands))

But, as far as I understand, both queries can't be expressed in Criteria API (you can't write from p.ands in subquery and can't use in elements ). 但是,据我所知,两个查询都不能用Criteria API表示(您不能在子查询中使用from p.ands编写,也不能in elements使用)。 So, in Criteria API you have to use additional join (note 2 Parent s): 因此,在Criteria API中,您必须使用其他联接(注释2 Parent ):

from Parent p
where not exists (select c from Parent p2 join p2.ands c 
    where not <condition> and p = p2)

那分离不应该是合取吗?

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

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