java 时间戳的休眠条件查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14889741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate criteria query for timestamp
提问by Sarika.S
I have one table which has a column submitted_date(time-stamp without timezone). 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?
我有一张表,其中有一列 submit_date(time-stamp without timezone)。我需要列出表中具有特定日期作为提交日期的所有记录。但不考虑数据库中的时间。我通过使用条件查询和休眠来检索记录。如何忽略这里的时间?
Actually I pass a date from client side and has to retrieve the records that have the same date as submitted_date. But no need to consider the time.
实际上,我从客户端传递了一个日期,并且必须检索与 submit_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);
}
}
}
Above is my code. Client side is done using extjs. The server side code of extjs filtering for one date field is this.
以上是我的代码。客户端是使用 extjs 完成的。extjs 过滤一个日期字段的服务器端代码是这样的。
hibernate is given below.
下面给出了休眠。
<?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.
我在数据库中有两条记录,如下所示。
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?
我将如何进行忽略时间部分的查询?
回答by Akhil Raina
For this, you will have to apply a restriction that selects all dates between submittedDate and submittedDate+1.
为此,您必须应用一个限制来选择 submitDate 和 submitDate+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
在您的情况下,开始日期 = 2013-02-15 和结束日期 = 2013-02-16
回答by Nayan Wadekar
You can try below code, which utilizes native Oracle trunc
function to discard time while comparison.
你可以试试下面的代码,它利用原生的 Oracletrunc
函数在比较时丢弃时间。
criteria.add(Restrictions.sqlRestriction("trunc(submitted_date) = ?", submittedDate, org.hibernate.type.StandardBasicTypes.DATE));
You can modify the function according to the database provider.
您可以根据数据库提供者修改函数。
回答by Sumit Desai
Simply add a restriction to your criteria for property corresponding to column submitted_date. You can write it as
只需为与 submit_date 列相对应的属性的条件添加限制。你可以把它写成
criteria.add(Restrictions.eq("submittedDate"), '2013-02-15');