简体   繁体   中英

hibernate criteria for a time range search on a Date field

Im using Oracle 10g,Hibernate 3,springMVC, jqgrid. I have some Date fields in ORACLE which are mapped as follows

@Temporal(TemporalType.DATE)
@Column(name = "DOC_CREATION_DATE", length = 7,insertable=true,updatable=false)
public Date getDocCreationDate() {
    return this.docCreationDate;
}

In my grid I filter date using jqueryCalendar and everything is fine. now I have a new request from the client which is to show the time of documentCreation and they also want to be able to filter by a time range. for example:

find all records created between 6:am and 7:pm, find all records created at 6:am.

I have tried already formatting the date field with

@Temporal(TemporalType.TIMESTAMP)

and that's not what they want

Is there any other approach to this problem, any good ideas how to implement this are very welcome.

the thing is I need to use the same mapped field to query in one column of the Jqgrid for the regular date( 12/01/2012 using jqueryCalendar)and add another column for the time part of that same mapped field once that is done I need to query (hibernate criteria) the time column for a range of time something like this mock:

...
 criteria.add(Restrictions.ge("docCreationDate.mappedFieldTimePart",6AM ));

thank you for all the help

The column is of type mapped to a Time, and thus you must compare its value with a Time:

Time sixAM = Time.valueOf("06:00:00");
criteria.add(Restrictions.ge("docCreationDate.mappedFieldTimePart", sixAM));

You can also use a regular date:

Date sixAM = new SimpleDateFormat("HH:mm").parse("06:00");
criteria.add(Restrictions.ge("docCreationDate.mappedFieldTimePart", sixAM));

You must change the @Temporal annotation to use either TemporalType.TIMESTAMP or add another field and annotate it with TemporalType.TIME . Hibernate uses the @Temporal annotation to determine if the field is to be treated like a java.sql.Timestamp or a java.util.Date with the time lopped-off (set to midnight, or 00h 00m 00.0s). This allows developers to use java.util.Date everywhere in their application and never have to worry about the Timestamp class (it's banished from most of our codebase).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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