2

我有以下查询 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);

如何在第一个括号内写“和”和“或”?

4

2 回答 2

1

你想要的结构

((te.TeamId in (1) and ar.TeamReadOnly = 1) or te.TeamId is null)

将是这样的:

Restrictions.or(
    Restrictions.and(
        Restrictions.in(x, x), Restrictions.eq(x, x)
    ),
    Restrictions.eq(x,x)
)
于 2011-08-12T12:38:13.673 回答
0

您可以放置​​第一个and进行连接,然后放置第二个,例如:

//pseudo code
tablea.foreignId = id

tableb.id = id
于 2011-08-09T10:54:41.350 回答