spring data jpa如何加入多个表?

时间:2018-04-10 12:42:13

标签: spring-data-jpa

现在我使用spring数据jpa来开发。 不,我有问题。关系是打击: 表a具有表b的id,表b具有表c的id。

 CREATE TABLE testa
 (
    a_id UUID NOT NULL,
    name CHARACTER VARYING(256),
    b_id UUID,
    PRIMARY KEY (a_id)
 );
INSERT INTO testa (a_id, name, b_id) VALUES ('1673da5f-20a2-4959-82ff-d92db4e951d2','table a', '37c78baf-6b10-42b4-961d-da570c396050');

CREATE TABLE testb
(
    b_id UUID NOT NULL,
    name CHARACTER VARYING(256),
    c_id UUID,
    PRIMARY KEY (b_id)
);
INSERT INTO testb (b_id, name, c_id) VALUES ('37c78baf-6b10-42b4-961d-da570c396050','table b', '50169479-cf16-412a-9a6d-3bf19a111a0b');

CREATE TABLE testc
(
    c_id UUID NOT NULL,
    name CHARACTER VARYING(256),
    PRIMARY KEY (c_id)
);
INSERT INTO testc (c_id, name) VALUES ('50169479-cf16-412a-9a6d-3bf19a111a0b','table c');

我想实施:

select
   *
from
   testa a
left join
   testb b
on
   b.b_id = a.b_id
left join
   testc c
on
   c.c_id = b.c_id
where c.name = 'table c';

但是在春天jpa。

@Override
public Predicate toPredicate(Root<RecoveredResource> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Join<TESTA, TESTB> join = root.join("b", JoinType.LEFT);
Join<TESTB, TESTC> join2 = join.join("c", JoinType.LEFT);

这段代码不起作用。它有错误。 我不知道如何在spring data jpa中做到这一点

0 个答案:

没有答案