How best to persist / model dates with CMP?

Discussions

EJB design: How best to persist / model dates with CMP?

  1. How best to persist / model dates with CMP? (5 messages)

    Hi,

    I would be very interested in people's recommended ways of using CMP to store dates - or can anyone point me to a chapter of reading info / examples ?

    Also, I see that EBJ-QL dosen't support data operators in the WHERE clause for dates (even in 2.1) so how do you add date selection clauses to EJB-QL?

    Advice or links greatly appreciated.

    Many thanks,

    Andy
  2. Andrew,

    I would recommend that you use java.sql.Date as your date data type. Also check your app vendor or mapping docs.

    Here is a Finder mapping.

    java.util.Collection findActiveByDate(java.sql.Date effectivedate)

    <ejb-ql>
    SELECT OBJECT(b) FROM MyEJB as b where b.aDate = ?1
    </ejb-ql>

    For more specific datefunctions like dateadd, datepart, you might have to resort to proprietary app vendor support or use BMP.
  3. Hi,
    You could store the dates as longs (Date.getTime()).
    Best regards, Mircea
  4. Hi guys,

    Thanks for your replies. My dates need to store the time, not just the date. I then also need to be able to do sql where clauses based on date inclusions.

    I guess therefore that storing it as a Long might be best, and then passing Longs as where query values for the inclusion values.

    Roll on full EJB-QL date support!

    Thanks,

    Andy
  5. Andrew

    java.sql.Date is date and time. The Long is number of milliseconds from 1972. The java.sql.Date will make it easier to query db directly as well, since you can write an sql like this

        SELECT * from MyTable where mydate > '4/15/04 1:30:00PM' and mydate < '4/14/04 4:00:00PM'

    You can't realy do that with the long millisecond value

    Goodluck

    Mike
  6. reviise sql[ Go to top ]

    SELECT * from MyTable where mydate > '4/15/04 1:30:00PM' and mydate < '4/15/04 4:00:00PM'