简体   繁体   English

grails或休眠条件以仅按时间分量进行搜索

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

Given below is my Domain object. 下面给出的是我的Domain对象。

Game {
    Date startDate
}

Now I want to only search the games which start at hh:mm AM (dynamic search) in the morning regardless of date. 现在,我只想搜索早上从hh:mm AM(动态搜索)开始的游戏,而不管日期如何。 I am looking for Grails criteria something like this 我正在寻找像这样的Grails标准

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

Where timeValue = "10:30 AM" or anything like that. 其中timeValue =“ 10:30 AM”或类似的内容。

I am using Hibernate criteria due to some custom restrictions that aren't support in GORM, so it would be nice if anyone knows how to do it with Hibernate criteria. 由于GORM不支持某些自定义限制,因此我正在使用Hibernate标准,所以如果有人知道如何使用Hibernate标准,那就太好了。

Right now this is what I am trying to do in Hibernate Criteria and it doesn't work(I am using postgre). 现在,这是我在Hibernate Criteria中尝试做的事情,它不起作用(我正在使用postgre)。

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

Highly appreciate your help. 非常感谢您的帮助。

You can use HibernateCriteriaBuilder. 您可以使用HibernateCriteriaBuilder。 The builder can be retrieved through the "createCriteria()" dynamic static method of Grails domain classes: 可以通过Grails域类的“ createCriteria()”动态静态方法来检索构建器:

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

The builder can also be instantiated standalone with a SessionFactory and persistent Class instance: 也可以使用SessionFactory和持久性Class实例独立实例化该构建器:

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

You can get a Date instance with only time and use that in Hibernate Criteria API to query. 您可以获取只有时间的Date实例,并在Hibernate Criteria API中使用该实例进行查询。

Compare date's date part only with Timestamp in Hibernate 仅将日期的日期部分与Hibernate中的时间戳进行比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM