gpt4 book ai didi

java - JPA Criteria API - 如何选择,如 "field not in"

转载 作者:行者123 更新时间:2023-12-01 13:04:56 25 4
gpt4 key购买 nike

我有以下情况。

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评论和这个链接:"Not in" constraint using JPA criteria

编辑后我意识到目标略有不同(请参阅编辑后的帖子)。

所以必须使用子查询,参见:JPQL, How to NOT select something

对于任何人来说,这里是对我有用的 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));

希望这对某人有帮助! :-)

关于java - JPA Criteria API - 如何选择,如 "field not in",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23339895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com