Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA标准API-如何选择“相似”;字段不在";_Java_Jpa_Criteria Api - Fatal编程技术网

Java JPA标准API-如何选择“相似”;字段不在";

Java JPA标准API-如何选择“相似”;字段不在";,java,jpa,criteria-api,Java,Jpa,Criteria Api,我有以下情况 public class TestExecution { private Collection<TestExecutionTag> testExecutionTags; } public class TestExecutionTag { private Tag tag; } public class Tag { private String name; } 我会把它描述为 Select testExecution WHER

我有以下情况

public class TestExecution {

    private Collection<TestExecutionTag> testExecutionTags;

}


public class TestExecutionTag {


    private Tag tag;

}


public class Tag {

    private String name;

}
我会把它描述为

Select testExecution WHERE testExecution.testExecutionTag.tag.name NOT IN (<ArrayList of String values>).
选择testExecution,其中testExecution.testExecutionTag.tag.name不在()。
这样的事情可能吗


编辑:很抱歉,我没有正确指定它,因为我认为它不相关。整个目标实际上是选择带有特定标记的测试执行,但从中排除包含其他标记的测试执行

我终于得到了答案,但非常感谢@Priyesh评论和这个链接:

编辑之后,我意识到这与目标没有什么不同(参见编辑后的文章)

因此必须使用子查询,请参见:

对于任何人来说,这里是对我有用的CriteriaAPI查询,但它基本上只是上面重写的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));
谓词wantedToBePresentTags=cb.lower(rTag.get(“name”)).in(cb.parameter(List.class,“tagList”);
Subquery sq=criteriaQuery.Subquery(TestExecution.class);
根sqRoot=sq.from(TestExecution.class);
Join sqTag=sqRoot.joinCollection(“testExecutionTags”).Join(“tag”);
sq.select(sqRoot.get(“id”));
sq.where(cb.lower(sqTag.get(“name”)).in(cb.parameter(List.class,“excludedTagList”));
谓词excludedTags=cb.not(rExec.get(“id”).in(sq));
...
其中(cb.和(wantedToBePresentTags,不包括dTags));

希望这有助于某人!:-)

你查过了吗?谢谢你的回复!事实上,我确实试过了。仍然不起作用:-(如果我发布生成的JPQL查询会有帮助吗?不幸的是,我认为忽略整个代码是没有帮助的,因为它非常复杂。我的目标是:选择那些有特定标记但排除有其他标记的测试执行,比如+tag1+tag2-mustNotBePresentTag。我有带有所需标记的谓词。)s和与builder.and()链接的确切查询。是否看到任何错误?
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));