Hibernate Criteria Query – 嵌套条件
我无法弄清楚如何使用Hibernate Criteria synthax创建这样的查询
select * from x where xa = 'abc' and (xb = 'def' or xb = 'ghi')
你知道怎么做吗?
我正在使用Hibernate Restriction静态方法,但我不明白如何指定嵌套的’或’条件
您的具体查询可能是:
crit.add(Restrictions.eq("a", "abc")); crit.add(Restrictions.in("b", new String[] { "def", "ghi" });
如果您对AND和OR一般感到疑惑,请执行以下操作:
// Use disjunction() or conjunction() if you need more than 2 expressions Disjunction aOrBOrC = Restrictions.disjunction(); // A or B or C aOrBOrC.add(Restrictions.eq("b", "a")); aOrBOrC.add(Restrictions.eq("b", "b")); aOrBOrC.add(Restrictions.eq("b", "c")); // Use Restrictions.and() / or() if you only have 2 expressions crit.add(Restrictions.and(Restrictions.eq("a", "abc"), aOrBOrC));
这相当于:
where xa = 'abc' and (xb = 'a' or xb = 'b' or xb = 'c')
使用更像(xb IN ('def', 'ghi'))
的语法。
- Hibernate遵循哪种模式?
- Hibernate:如何只获取非逻辑删除的对象
- 这些实体的equals和hashCode(Spring MVC + Hibernate)
- Hibernate持续vs保存
- Hibernate使用orphanRemoval触发约束违规
- getSingleResult在hibernate中返回本机标量查询的代理
- ClassCastException:org.springframework.orm.jpa.EntityManagerHolder无法强制转换为org.springframework.orm.hibernate5.SessionHolder
- Spring @Transactional Annotation:自我调用
- 正确使用Entity和DTO以在Restful Web服务中提供Json