hibernate条件查询

时间:2011-08-09 10:32:35

标签: hibernate hibernate-criteria

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

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

2 个答案:

答案 0 :(得分: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)
)

答案 1 :(得分:0)

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

//pseudo code
tablea.foreignId = id

tableb.id = id