简体   繁体   中英

Hibernate Criteria for Time > 0

I am to fix an older project, in which there is a database query gone wrong. In the Table, there is a field

viewTime TIME NOT NULL DEFAULT 0

I need to filter out the rows that actually have 0 as their viewTime :

Criteria query = /* create criteria */;
query.add(Restrictions.gt("viewTime", 0));

However, since viewTime is defined as a Date:

@Temporal(TemporalType.TIME)
private Date viewTime;

I get a casting exception. On the other hand, I have no idea how to create a valid Date object that represents time 0 . I can't change the type of the field as well for this.

Any way I can express viewTime > 0 in this Criteria object?

I think you have to compare date object with (00/00/00) but the any API will not produce DATE value like it.

This might your solution convert to null refer this link

It seems to me, that you can try such construction:

Criteria query = /* create criteria */;
query.add(Restrictions.gt("viewTime", new Date(0)));

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