1

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?

Lanbo
  • 15,118
  • 16
  • 70
  • 147

2 Answers2

2

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

Community
  • 1
  • 1
bNd
  • 7,512
  • 7
  • 39
  • 72
  • Though this solves it only from DB to Java, not the other direction. – Lanbo Jun 04 '13 at 12:52
  • @LambdaDusk I know but here when you do `new Date(0)` then it will return some date value not 0. it does not give error.but Is this equal to zero value? – bNd Jun 04 '13 at 13:07
  • Well you're right, but `null` doesn't either. I wish I could just see the generated SQL. – Lanbo Jun 04 '13 at 13:35
1

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

Criteria query = /* create criteria */;
query.add(Restrictions.gt("viewTime", new Date(0)));
Andremoniy
  • 34,031
  • 20
  • 135
  • 241