我有一个像这样的简单查询。
select id,status,c01,c02,c03 from mytable
where
criterion1
and criterion2
and criterion3
我需要添加另一个这样的过滤器
//In Java
if(mytable.status=1){
criteria.add(Restrictions.eq("anotherFilter",anotherFilterValue));
}
但是状态值出现在查询中,我的意思是我可以在内存中进行过滤,但是我希望能够直接在数据库中进行过滤
像订购用例一样,我可以使用某种方法来实现吗?
喜欢
select id,status,c01,c02,c03 from mytable
where
criterion1 = ?
and criterion2 = ?
and criterion3 = ?
and
case status is null
? anotherFilter = :valueToFilter
: 1=1//IS NOT NULL NOTHING TO DO..
最佳答案
这可以为您提供帮助。
select id,status,c01,c02,c03 from mytable
where
criterion1 = ?
and criterion2 = ?
and criterion3 = ?
and
((status is null and anotherFilter = :valueToFilter)
or (status is not null))
对于标准API
//In Java
criteria.add(
Restrictions.or(
Restrictions.isNotNull("status"),
Restrictions.and(
Restrictions.eq("anotherFilter",anotherFilterValue),
Restrictions.isNull("status")
)
)
);
关于java - Java Hibernate基于条件的空性的条件准则是否可能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43323175/