What is better way to work with time, using Hibernate Criteria and Oracle?

半世苍凉 提交于 2019-12-05 22:05:00
Selector

I find answer. Just need use sqlRestriction.

criteria.add(
    Restrictions.sqlRestriction(
        " NUMTODSINTERVAL(EVENT_DATE - TRUNC(EVENT_DATE), 'DAY') BETWEEN  NUMTODSINTERVAL (?,'SECOND') AND NUMTODSINTERVAL (?,'SECOND')",
        new Object[] { secondsForBegin, secondsForEnd },
        new Type[]   { StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER }));

Hibernate HQL has hour, minute and second functions that you can use in this particular case, so you can do this in HQL, rather than SQL. So you'll need an HQL query instead of a Criteria object. The HQL will look something like

from  tableName
where eventDate between :minDate and :maxDate
  and 3600 * hour( eventDate ) + 60 * minute( eventDate ) + second( eventDate )
      between :minSeconds and :maxSeconds

where you'll set the :minSeconds and :maxSeconds parameters appropriately. Watch out for out-by-one errors with :maxDate too.

More information on HQL can be found at http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html

You'll have to use a SQL restriction to do that, or, preferrably, you'll have to create your own Criterion implementation, which will issue the appropriate SQL restriction.

Use the org.hibernate.criterion.IlikeExpression class as an exemple: when it's not using the PostgreSQL dialect, it generates the SQL expression lower(columnName) like ?, where the argument of the expression is value.toString().toLowerCase().

Your criterion should generate something like to_date(to_char(columnName, 'HH24:MI:SS'), 'HH24:MI:SS') < ?) where the argument of your expression is your max time. (And of course, do a similar criterion for the min time)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!