简体   繁体   English

hibernate条件查询时间戳

[英]hibernate criteria query for timestamp

I have one table which has a column submitted_date(time-stamp without timezone). 我有一个表有一个submit_date列(没有时区的时间戳)。 I need to list all records in the table having a particular date as submitted date. 我需要列出表中具有特定日期作为提交日期的所有记录。 But do not consider the time in database. 但是不要考虑数据库中的时间。 I retrieve the records by using criteria query and hibernate. 我通过使用条件查询和休眠来检索记录。 How to ignore the time here? 如何忽略这里的时间?

Actually I pass a date from client side and has to retrieve the records that have the same date as submitted_date. 实际上我从客户端传递了一个日期,并且必须检索与submitted_date具有相同日期的记录。 But no need to consider the time. 但没有必要考虑时间。

    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);
                                }
                            }
}

Above is my code. 以上是我的代码。 Client side is done using extjs. 客户端使用extjs完成。 The server side code of extjs filtering for one date field is this. 对于一个日期字段,extjs过滤的服务器端代码是这个。

hibernate is given below. 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>

I have two records in database having submitted_date as below. 我有两个记录在数据库中的submission_date如下所示。

2013-02-15 00:00:00 2013-02-15 00:00:00

2013-02-15 13:04:42.787 2013-02-15 13:04:42.787

When I make a query to filter records having date as today, the first record with submitted date 2013-02-15 00:00:00 is only retrieved. 当我查询过滤日期为今天的记录时,仅检索提交日期为2013-02-15 00:00:00的第一条记录。

this is because the date object I used to query, has also this value '2013-02-15 00:00:00' 这是因为我用来查询的日期对象,也是这个值'2013-02-15 00:00:00'

How will I make a query which will ignore the time part? 如何进行忽略时间部分的查询?

For this, you will have to apply a restriction that selects all dates between submittedDate and submittedDate+1. 为此,您必须应用一个限制,选择submittedDate和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));

In your case, startDate = 2013-02-15 and end date = 2013-02-16 在您的情况下,startDate = 2013-02-15和结束日期= 2013-02-16

You can try below code, which utilizes native Oracle trunc function to discard time while comparison. 您可以尝试下面的代码,它利用本机Oracle trunc函数来比较时丢弃时间。

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

You can modify the function according to the database provider. 您可以根据数据库提供程序修改该功能。

Simply add a restriction to your criteria for property corresponding to column submitted_date. 只需在与submission_date列对应的属性条件中添加限制。 You can write it as 你可以把它写成

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

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

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