tags:

views:

313

answers:

1

I'm trying to get the books that were loaned between two dates. Since data has the lovely 2009 is shown as 109 feature I decided to use calendars.

However when writing my HQL I ran into the problem that BETWEEN doesn't view a Calendar as a date. Now I'm wondering if there's a solution for this. Or am I stuck writing functions in my class to get the hour, day, month, year and write a long where statement?

+1  A: 

You can get a Date from a Calendar using Calendar.getTime - does that help you? You may run into time zone issues if you're not careful, admittedly... how exactly are the dates stored in the database?

Jon Skeet
You can use getTime in a java class, but you can't do so in HQL.
Okay, I'm confused as to how you're calling this then... I thought you were passing in the arguments to the query. Could you give some sample code in your question to make it clearer? I suspect I may not be able to help, but hopefully someone else can.
Jon Skeet
query = session.createQuery("from model.Book book where book.loaned between :earliest and :latest").The problem is that the between only works with a date object. and loaned is Java.Util.Calendar.There might be another way to get the books between the two dates, but I haven't found it yet.
Please edit that code into your question - it'll make it easier for everyone reading the question. However, I would suggest that book.loaned should probably be a java.util.Date; you can add an extra (non-persistent) property to get it as a calendar.
Jon Skeet
Can you rewrite it to use less/greater, e.g. : where book.loaned >= :earliest and book.loaned <= :latest?
nos