假设我有三个表:
CREATE TABLE "C" (
id bigint NOT NULL,
cc integer,
CONSTRAINT "C_ID" PRIMARY KEY (id)
);
ALTER TABLE "C" OWNER TO ***;
CREATE TABLE "B" (
id bigint NOT NULL,
c_id bigint references "C"(id),
bb integer,
CONSTRAINT "B_ID" PRIMARY KEY (id)
);
ALTER TABLE "B" OWNER TO ***;
CREATE TABLE "A" (
id bigint NOT NULL,
c_id bigint references "C"(id),
aa integer,
CONSTRAINT "A_ID" PRIMARY KEY (id)
);
ALTER TABLE "A" OWNER TO ***;
我需要从表 A 中选择具有公共外键 (c_id) 的行与表 B 中的行。不过,我有以下 SQL 查询
SELECT "A".aa AS "AA", "B".bb AS "BB", "C".id AS "C_ID"
FROM "A"
INNER JOIN "C" on "C".id = "A".c_id
INNER JOIN "B" on "C".id = "B".c_id;
在 Java 中
@Entity
class A {
...
@ManyToOne
C cObj;
...
}
@Entity
class B {
..
@ManyToOne
C cObj;
..
}
@Entity
class C {
..
}
所以问题:如何编写等效的 Criteria API 查询?谢谢。