我有以下HQL,我想知道它如何处理比较日期列为空。例如,如果给定的时间参数为空,会发生什么情况。我检查并得到空的结果。情况总是如此吗?有记录吗?休眠HQL比较时间戳为空
select mo from MyClassMO mo
where mo.creationDate is not null and mo.creationDate >= (:givenTime)
什么,如果givenTime取代,后者返回null内选择查询? 感谢
我有以下HQL,我想知道它如何处理比较日期列为空。例如,如果给定的时间参数为空,会发生什么情况。我检查并得到空的结果。情况总是如此吗?有记录吗?休眠HQL比较时间戳为空
select mo from MyClassMO mo
where mo.creationDate is not null and mo.creationDate >= (:givenTime)
什么,如果givenTime取代,后者返回null内选择查询? 感谢
如果你的参数是空的,你必须手动管理。
在您的应用逻辑的基础上,您可以编写查询。
我想某些情况下(告诉我,如果你的是这些或另一个)。
案例1:不考虑givenTime过滤器
您可以WIRTE此查询:
String hql = "select mo from MyClassMO mo" +
" where mo.creationDate is not null";
if (givenTime != null) {
hql += " and mo.creationDate >= (:givenTime)";
}
案例2:如果givenTime为null,把当前的日期
String hql = "select mo from MyClassMO mo" +
" where mo.creationDate is not null " +
" and mo.creationDate >= COALESCE(:givenTime, current_date())";
案例3:如果givenTime是子查询返回NULL,把当前日期
String hql = "select mo from MyClassMO mo" +
" where mo.creationDate is not null " +
" and mo.creationDate >= "
" (SELECT COALESCE(:giventime, current_date()) " +
" FROM yourtable WHERE conditions";
以及如果给定时间替换为返回null的内部选择查询? – user1116377
我知道你问的HQL,但处理这样的情况下,我更喜欢使用一些所谓的标准中休眠。它可以与SQL轻松混合。
JPA and Hibernate - Criteria vs. JPQL or HQL
Criteria criteria = session
.createCriteria(MyClassMO.class)
.add(Restrictions.isNotNull("creationDate"));
if (givenTime!=null){
criteria.add(Restrictions.ge("creationDate", givenTime));//ge - greater equals
}
List<MyClassMO> result = criteria.list();
以及如果将givenTime替换为返回null的内部select查询会怎么样? – user1116377
如果givenTime为NULL,则必须手动管理。 –