休眠条件查询时间戳 [英] hibernate criteria query for timestamp
问题描述
我有一个表格,其中有一个列submitted_date(没有时区的时间戳)。我需要列出表中具有特定日期作为提交日期的所有记录。但不要考虑数据库中的时间。我通过使用标准查询和休眠来检索记录。如何忽略这里的时间?
实际上,我从客户端传递一个日期,并且必须检索与submitted_date具有相同日期的记录。但不需要考虑时间。
else if(extjsFilter.getField()。equals(submittedDate)){
String str_date = extjsFilter.getValue();
SimpleDateFormat format1 =新的SimpleDateFormat(MM / dd / yyyy);
SimpleDateFormat format2 = new SimpleDateFormat(yyyy-MM-dd);
日期date2 = format1.parse(str_date);
String datenew = format2.format(date2);
日期日期=(日期)format2.parse(datenew);
if(extjsFilter.getType()。equals(date))
{
if(extjsFilter.getComparison()。equals(gt))
{
过滤器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映射DTD // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0。 DTD>
< hibernate-mapping package =com.hiringsteps.ats.job.domain>
name =Job
table =hs_job_master>
< id name =idcolumn =job_idunsaved-value =null>
< generator class =sequence>
< param name =sequence> hs_job_id_seq< / param>
< / generator>
< / id>
< property name =submittedDatecolumn =submitted_date/>
< filter name =jobFilterPostDateGT><![CDATA [submitted_date> :postDateFilterGT]>< /滤光器>
< filter name =jobFilterPostDateLT><![CDATA [submitted_date< :postDateFilterLT]>< /滤光器>
< filter name =jobFilterPostDateEQ><![CDATA [:postDateFilterEQ = submitted_date]]>< / filter>
< / class>
< filter-def name =jobFilterPostDateGT>
< filter-param name =postDateFilterGTtype =date/>
< / filter-def>
< filter-def name =jobFilterPostDateLT>
< filter-param name =postDateFilterLTtype =date/>
< / filter-def>
< filter-def name =jobFilterPostDateEQ>
< filter-param name =postDateFilterEQtype =date/>
< / filter-def>
< / hibernate-mapping>
我在数据库中有两条记录,其中submit_date如下。
2013-02-15 00:00:00
2013-02-15 13:04:42.787
当我进行查询以筛选具有如今日期的记录时,只会检索提交日期为
2013-02-15 00:00:00的第一条记录。
这是因为我用来查询的日期对象,也有这个值
'2013-02-15 00:00:00'
我如何做出一个会忽略时间部分的查询? 为此,您将不得不应用限制选择submitDate和submittedDate + 1之间的所有日期。
//重新开始日期0000小时和结束日期之间的日期0000小时
criteria.add(Restrictions.ge (startDate,sDate));
criteria.add(Restrictions.lt(endDate,eDate));
在您的情况下,startDate = 2013-02-15
和结束日期= 2013- 02-16
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?
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.
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.
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 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.
this is because the date object I used to query, has also this value '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.
//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
这篇关于休眠条件查询时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!