CriteriaBuilder:使用ON子句一对多连接 [英] CriteriaBuilder: join one-to-many with ON clause

查看:219
本文介绍了CriteriaBuilder:使用ON子句一对多连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您具有以下一对多关系:学校->学生-> ScientificWork 。现在,您要选择所有学生名为 John且其科学工作称为 Black Holes的学校。

Suppose you the following OneToMany relationships: School->Student->ScientificWork. Now you want to select all Schools that have Student with name 'John' and his scientific job is called 'Black Holes'.

我这样做如下,但是由于某种原因,它使我所有可能的学校都退学了。

I do it like the following, but for some reason it retrurns me all possible schools.

public static Specification<School> spec() {
    return (root, query, cb) -> {
        final SetJoin<School, Student> studs = root.joinSet("students", JoinType.LEFT);
        final SetJoin<Student, ScientificWork> works = root.joinSet("works", JoinType.LEFT);
        return cb.and(
                cb.equal(studs.get(Student_.name), 'John'),
                cb.equal(nodes.get(ScientificWork_.name), 'Black Holes')
        );
    };
}



更新



找到此答案我尝试了以下操作,但结果相同(返回的是所有学校而不是一个学校):

Update

After finding this answer I tried the following, but with the same result (it returns me all Schools instead of one):

public static Specification<School> spec() {
    return (root, query, cb) -> {
        final SetJoin<School, Student> studs = root.joinSet("students", JoinType.LEFT);
        studs.on(cb.equal(studs.get(Student_.name), 'John'));
        final SetJoin<Student, ScientificWork> works = root.joinSet("works", JoinType.LEFT);          
        return cb.equal(nodes.get(ScientificWork_.name), 'Black Holes');
    };
}


推荐答案

public static Specification<School> spec() {
return (root, query, cb) -> {
    final Join<School, Student> studs = root.join("students", JoinType.LEFT);
    studs.on(cb.equal(studs.get(Student_.name), "John"));
    final Join<Student, ScientificWork> works = studs.join("works", JoinType.LEFT);          
    return cb.equal(works.get(ScientificWork_.name), "Black Holes");
};

}

我使用了加入而不是 joinSet ,然后将 ** works **。get(ScientificWork_.name)代替 ** nodes **。get(ScientificWork_.name)

I used join instead of joinSet and put **works**.get(ScientificWork_.name) instead of **nodes**.get(ScientificWork_.name)

这篇关于CriteriaBuilder:使用ON子句一对多连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆