2013-02-15 121 views
2

我有一个表,其中有列submitted_date(时间戳无时区)。我需要列出表中具有特定日期作为提交日期的所有记录。但不要考虑数据库中的时间。我通过使用标准查询和休眠来检索记录。如何忽略这里的时间?休眠条件查询时间戳

其实我从客户端传递一个日期,并且必须检索与submitted_date具有相同日期的记录。但不需要考虑时间。

else if(extjsFilter.getField().equals("submittedDate")) { 
          String str_date=extjsFilter.getValue(); 
          SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy"); 
          SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd"); 
          Date date2 = format1.parse(str_date); 
          String datenew = format2.format(date2); 
          Date date = (Date)format2.parse(datenew); 

          if(extjsFilter.getType().equals("date")) 
          { 
           if(extjsFilter.getComparison().equals("gt")) 
           { 
            Filter postDateFilterGT = getSession().enableFilter("jobFilterPostDateGT"); 
            postDateFilterGT.setParameter("postDateFilterGT", date); 
           } 
           if(extjsFilter.getComparison().equals("lt")) 
           { 
            Filter postDateFilterLT = getSession().enableFilter("jobFilterPostDateLT"); 
            postDateFilterLT.setParameter("postDateFilterLT", date); 
           } 
           if(extjsFilter.getComparison().equals("eq")) 
           { 
            Filter postDateFilterEQ = getSession().enableFilter("jobFilterPostDateEQ"); 
            postDateFilterEQ.setParameter("postDateFilterEQ", date); 
           } 
          } 
} 

以上是我的代码。客户端使用extjs完成。 extjs过滤一个日期字段的服务器端代码是这样的。

hibernate在下面给出。

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="com.hiringsteps.ats.job.domain"> 
    <class 
     name="Job" 
     table="hs_job_master"> 

     <id name="id" column="job_id" unsaved-value="null"> 
      <generator class="sequence"> 
       <param name="sequence">hs_job_id_seq</param> 
      </generator> 
     </id> 

     <property name="submittedDate" column="submitted_date"/>     

     <filter name="jobFilterPostDateGT"><![CDATA[submitted_date > :postDateFilterGT]]></filter> 
     <filter name="jobFilterPostDateLT"><![CDATA[submitted_date < :postDateFilterLT]]></filter> 
     <filter name="jobFilterPostDateEQ"><![CDATA[:postDateFilterEQ = submitted_date]]></filter> 
    </class>  

    <filter-def name="jobFilterPostDateGT"> 
     <filter-param name="postDateFilterGT" type="date"/> 
    </filter-def> 
    <filter-def name="jobFilterPostDateLT"> 
     <filter-param name="postDateFilterLT" type="date"/> 
    </filter-def> 
    <filter-def name="jobFilterPostDateEQ"> 
     <filter-param name="postDateFilterEQ" type="date"/> 
    </filter-def> 
</hibernate-mapping> 

我在数据库中有两条记录,其中submitted_date如下。

2013年2月15日00:00:00

2013年2月15日13:04:42.787

当我做一个查询过滤使日期为今天的记录,以第一名的成绩提交日期 2013-02-15 00:00:00只能被检索。

这是因为我用来查询的日期对象,也有这个值 “2013年2月15日00:00:00”

我将如何进行查询,将忽略时间部分?

+0

可以请你解释一下'但是不考虑数据库中的时间'是什么意思? – 2013-02-15 07:08:53

+0

你试过了吗?你的问题不清楚 – 2013-02-15 07:09:58

+0

不需要考虑什么时间?系统时间还是db时间?执行时间? – aksappy 2013-02-15 07:12:45

回答

5

为此,您必须应用一个限制,选择submitDate和submittedDate + 1之间的所有日期。

//Resticts the dates between start date 0000 hrs and end date 0000 hrs 
criteria.add(Restrictions.ge("startDate", sDate)); 
criteria.add(Restrictions.lt("endDate", eDate)); 

在你的情况,的startDate = 2013年2月15日 和结束日期= 2013年2月16日

+1

我正在使用休眠过滤器。我将如何与过滤器做到这一点? – 1355 2013-02-15 08:39:25

+0

我避免使用过滤器,并可以与此工作。 – 1355 2013-02-15 10:42:01

+0

我有同样的问题,我必须用相同的逻辑解决它找到第二天,并获取记录通用电气'日期'和LT'nextdate' – JegsVala 2016-02-10 09:44:38

1

只需在对应于列submitted_date的属性条件中添加限制即可。您可以将其编写为

criteria.add(Restrictions.eq("submittedDate"), '2013-02-15'); 
+0

日期参数应该是你想要比较你的时间的那个参数。 – 2013-02-15 07:13:43

+0

我再次更新了这个问题。你可以请看看它 – 1355 2013-02-15 07:30:59

4

你可以试试下面的代码,它利用本地Oracle trunc功能丢弃的时间,同时比较。

criteria.add(Restrictions.sqlRestriction("trunc(submitted_date) = ?", submittedDate, org.hibernate.type.StandardBasicTypes.DATE)); 

您可以根据数据库提供者修改函数。