Re: EJB-QL Question: Date

JP Lorandi <[email protected]> Mon, 18 Jul 2005 13:47:06 +0100
Newsgroups gmane.comp.java.sun.ejb.general
Message-ID <[email protected]>
mcrod wrote:

>Hi,
>
>I want to query a CMP that has a field Date mapped to an Oracle Date col
>table. The data is stored in the database with year, month, day and time.
>What I want is, return all registers with a certain year, month and day. Not
>considering the time. The signature must be:
>
>java.util.Collection findByDate(java.util.Date date)
>
>So, as the Date I am passing as a parameter has an empty time, and the time
>is present in the database field, how to build a query that ignores it ?
>
>
On EJB 2.1, you can have:

java.util.Collection findByDate(java.util.Date dateBeg, java.util.Date dateEnd)

And an EQL Query with a predicate:

WHERE myDateField BETWEEN ?1 AND ?2

The code that calls the method:

Date date = ......
MyHome home = .....
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(HOUR_OF_DAY, 0);
c.set(MINUTE, 0);
c.set(SECOND, 0);
c.set(MILLISECOND, 0);
Date dateBeg = c.getTime();
c.add(DAY_OF_MONTH, 1);
Date dateEnd = c.getTime();

Collection bag = home.findByDate(dateBeg, dateEnd);

Also, you can, depending on your server/db, create a SQL statement to do
the same, but it depends a lot on your config
and thus it's not portable.

HTH,
JP

===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[email protected] and include in the body of the message "help".