使用Hibernate Criteria和Oracle,什么是更好的工作方式? [英] What is better way to work with time, using Hibernate Criteria and Oracle?

查看:85
本文介绍了使用Hibernate Criteria和Oracle,什么是更好的工作方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按时间选择实体。
我使用包含日期和时间的DATE字段类型的Oracle DBMS。这是我的代码。如何按时间标准选择数据?

I want to select entity by Time. I have Oracle DBMS with field type DATE which contain date and time. This is my code. How to select data by Time criteria?

Calendar timeCal = new GregorianCalendar(0,0,0,0,0,0);
Date     timeMin = timeCal.getTime();

timeCal.add(Calendar.HOUR_OF_DAY, 1);
Date     timeMax = timeCal.getTime();

if (minDate != null && maxDate != null)
    criteria.add(Restrictions.between("eventDate", minDate, maxDate));

if (onDate != null) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(onDate);
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    criteria.add(Restrictions.between("eventDate", onDate, calendar.getTime()));
}

if(minTime!=null&&maxTime!=null){
    /*
      How to add Restriction on eventDate field, for time only?
      if minTime is 3:00 and maxTime is 16:00, must to return
      all rows having the time part of their eventDate between 
      3:00 and 16:00, regardless of the date part
    */
}


推荐答案

我找到答案。只需要使用sqlRestriction。

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 Criteria和Oracle,什么是更好的工作方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆