jorgen
jorgen

Reputation: 1257

hibernate criteria query

I have the following query sql query:

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)

This is what I've written so far:

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);

How can I write the "and" inside the first brackets and the "or"?

Upvotes: 2

Views: 1369

Answers (2)

EkcenierK
EkcenierK

Reputation: 1439

The structure you want for

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

will be something like:

Restrictions.or(
    Restrictions.and(
        Restrictions.in(x, x), Restrictions.eq(x, x)
    ),
    Restrictions.eq(x,x)
)

Upvotes: 1

ssedano
ssedano

Reputation: 8432

You can place a first and to do the join and a second one like:

//pseudo code
tablea.foreignId = id

tableb.id = id

Upvotes: 0

Related Questions