JPA 2.0在where子句中有多个OR

时间:2011-06-27 08:24:21

标签: java hibernate jpa jpa-2.0

无法找到这个答案。我们有一个条件查询,应该沿着这些行输出SQL:

select * from x where x.one is null and 
( 
 (x.two = 2 and x.three = 3) or
 (x.two = 3 and x.three = 4) or
 (x.two = 5 and x.three = 6) or 
 etc
 etc
)

我们正在使用Hibernate 3.6.5。首先,我们收集列表中的添加内容,然后我们继续添加这些内容,如下所示:

List<Predicate> ands = new ArrayList<Predicate>();
ands.add(cb.and(cb.equal(attrExpr, 2), cb.equal(attrExpr, 2)));
ands.add(cb.and(cb.equal(attrExpr, 3), cb.equal(attrExpr, 3)));

predicates.add(cb.or(ands.toArray(new Predicate[ands.size()])));

其中cb是CriteriaBuilder,attrExpr是x.two的路径。生成的SQL如下:

from
    ccp_organization organizati0_ 
left outer join
    ccp_orgattribute_org attributes1_ 
        on organizati0_.id=attributes1_.organization_id 
where
    (
        organizati0_.deleted_=? 
        or organizati0_.deleted_ is null
    ) 
    and (
        attributes1_.id=2 
        and attributes1_.id=2 
        or attributes1_.id=3 
        and attributes1_.id=3 
        or attributes1_.id=4 
        and attributes1_.id=4
    )

它应该是:

from
    ccp_organization organizati0_ 
left outer join
    ccp_orgattribute_org attributes1_ 
        on organizati0_.id=attributes1_.organization_id 
where
    (
        organizati0_.deleted_=? 
        or organizati0_.deleted_ is null
    ) 
    and (
        (attributes1_.id=2 
        and attributes1_.id=2)
        or 
        (attributes1_.id=3 
        and attributes1_.id=3)
        or 
        (attributes1_.id=4 
        and attributes1_.id=4)
    )

0 个答案:

没有答案