我有以下查詢SQL查詢:休眠條件查詢
select * from Articles ar
left join ArticleTeamRelationships atr on atr.ArticleId = ar.ArticleId
left join Team te on te.TeamId = atr.TeamId
inner join ArticleCategoryRelationships acr on acr.ArticleId = ar.ArticleId
inner join ArticleCategory ac on ac.CategoryId = acr.CategoryId
where ac.CategoryId = 3
and ((te.TeamId in (1) and ar.TeamReadOnly = 1) or te.TeamId is null)
這是我到目前爲止已經寫的:
Criteria childCriteriaCategory = criteria.createCriteria("articleCategory", Criteria.INNER_JOIN);
childCriteriaCategory.add(Restrictions.like("categoryId", categoryId));
Criteria childCriteriaTeam = criteria.createCriteria("team", Criteria.LEFT_JOIN);
childCriteriaTeam.add(Restrictions.in("teamId", teamId));
Criterion teamReadOnly = Restrictions.eq("teamReadOnly", true);
criteria.add(teamReadOnly);
我怎麼能寫「和」第一個括號內和「或」?
你檢查過了嗎? http://www.roseindia.net/hibernate/examples/criteria/hibernate-criteria-and-or.html – Holm
是的,我見過它。我的問題是我需要在兩個表格之間放置「和」,我不知道如何做到這一點。 – jorgen
使用別名來引用表。你不需要所有這些變量。以流利的風格編寫時,它更具可讀性。 –