Re: EJB-QL Question: Date

2005-07-18 Thread JP Lorandi

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.


Re: EJB-QL Question: Date

2005-07-18 Thread mcrod
Yes, that's what I was trying to avoid... :(

I was looking for any way to do a similar thing inside the EQL. I'm using
EJB 2.0, WLS 8.1.

thanks


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

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 ?



===
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.