[英]Hibernate query criteria for join between 3 tables
我有一個SQL查詢:
select * from A
INNER JOIN B
ON A.id = B.id
INNER JOIN C
ON B.id = C.id
INNER JOIN D
ON C.id = D.id
where D.name = 'XYZ'
and D.Sex = 'M'
我一直在嘗試為上述sql引入休眠查詢條件,但是有問題。 任何人都可以幫忙。
Criteria c = session.createCriteria(A.class, "a");
.createAlias("a.b", "b")
.createAlias("b.c", "c")
.createAlias("c.d", "d")
.add(Restrictions.eq("d.sex", "M"))
.add(Restrictions.eq("d.name", "XYZ"));
關於您的問題,您想要執行笛卡爾聯接 ,盡管條件可以使用HQL進行,但Criteria不支持此操作,如下所示。 還有一個類似的問題在這里
使用HQL查詢,您可以執行以下操作:
select a from
A a,
B b,
C c
where
a.id = b.id and
c.id = b.id and
d.id = c.id and
d.name = 'XYZ' and
d.sex = 'M'
該查詢用於常規的休眠查詢中:
Query query = session.createQuery(query); // <-- here you use the query above
List results = query.list();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.