簡體   English   中英

Hibernate HQL比較時間戳為null

[英]Hibernate HQL compare timestamp to null

我有以下HQL,我想知道它如何處理將date列與null進行比較。 例如,如果給定時間參數為null,會發生什么情況。 我檢查並得到空結果。 總是這樣嗎? 有文件記錄嗎?

select mo from MyClassMO mo
where mo.creationDate is not null and mo.creationDate >= (:givenTime)

以及如果給定的時間替換為內部選擇查詢並返回null,該怎么辦? 謝謝

我知道您問過有關hql的問題,但是對於處理這種情況,我更喜歡在休眠狀態下使用稱為Criteria的標准。 它可以與SQL輕松混合。

JPA和Hibernate-條件與JPQL或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();

如果您的參數可以為空,則必須手動進行管理。

在您應用邏輯的基礎上,您可以編寫查詢。

我想有些情況(如果您是其中之一,請告訴我)。

情況1:不考慮過濾器中的givenTime

您可以查詢以下查詢:

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:如果給定時間在子查詢中返回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";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM