簡體   English   中英

grails或休眠條件以僅按時間分量進行搜索

[英]grails or hibernate criteria to search only by time component

下面給出的是我的Domain對象。

Game {
    Date startDate
}

現在,我只想搜索早上從hh:mm AM(動態搜索)開始的游戲,而不管日期如何。 我正在尋找像這樣的Grails標准

Game.where {
    startDate.timePart == timeValue
}.list()

其中timeValue =“ 10:30 AM”或類似的內容。

由於GORM不支持某些自定義限制,因此我正在使用Hibernate標准,所以如果有人知道如何使用Hibernate標准,那就太好了。

現在,這是我在Hibernate Criteria中嘗試做的事情,它不起作用(我正在使用postgre)。

searchCriteria.add(Restrictions.sqlRestriction("to_timestamp(start_date, 'h:mm a') = ? ", timeValue, StandardBasicTypes.DATE))

非常感謝您的幫助。

您可以使用HibernateCriteriaBuilder。 可以通過Grails域類的“ createCriteria()”動態靜態方法來檢索構建器:

   def c = Game.createCriteria()
   def results = c {
     eq("start_date",timeValue)
   }

也可以使用SessionFactory和持久性Class實例獨立實例化該構建器:

  new HibernateCriteriaBuilder(clazz, sessionFactory).list {
     eq("start_date", timeValue)
  }

您可以獲取只有時間的Date實例,並在Hibernate Criteria API中使用該實例進行查詢。

僅將日期的日期部分與Hibernate中的時間戳進行比較

暫無
暫無

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

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