繁体   English   中英

JPA Criteria API-如何选择“字段不在”

[英]JPA Criteria API - how to select like “field not in”

我有以下情况。

public class TestExecution {

    private Collection<TestExecutionTag> testExecutionTags;

}


public class TestExecutionTag {


    private Tag tag;

}


public class Tag {

    private String name;

}

我现在想做的是使用标准的JPA Criteria API执行以下查询:

Select test executions (TestExecution) which don't have associated any of the tags (Tag) with name field value in provided list.

我会形容为

Select testExecution WHERE testExecution.testExecutionTag.tag.name NOT IN (<ArrayList of String values>).

这样的事情可能吗?

编辑:对不起,我没有正确指定它,因为我认为这无关紧要。 总体目标实际上是选择带有特定标签的测试执行,但从中排除包含其他标签的测试。

我终于得到了答案,但非常感谢@Priyesh注释和此链接: 使用JPA标准的“不参与”约束

编辑后,我意识到它的目标几乎没有什么不同(请参阅编辑后的帖子)。

因此必须使用子查询,请参见: JPQL,如何不选择某些内容

对于任何人来说,这都是对我有用的Criteria API查询,但基本上只是上面重写的JPQL查询。

Predicate wantedToBePresentTags = cb.lower(rTag.<String>get("name")).in(cb.parameter(List.class, "tagList"));

Subquery sq = criteriaQuery.subquery(TestExecution.class);
Root sqRoot = sq.from(TestExecution.class);
Join<TestExecution, Tag> sqTag = sqRoot.joinCollection("testExecutionTags").join("tag");
sq.select(sqRoot.get("id"));
sq.where(cb.lower(sqTag.<String>get("name")).in(cb.parameter(List.class, "excludedTagList")));

Predicate excludedTags = cb.not(rExec.get("id").in(sq));

...
criteriaQuery.where(cb.and(wantedToBePresentTags, excludedTags));

希望对您有所帮助! :-)

暂无
暂无

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

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