persistence@glassfish.java.net

Re: Using binary operators with Timestamp fields in EJBQL

From: Wouter van Reeven <wouter_at_van.reeven.nl>
Date: Tue, 30 Jan 2007 07:41:57 +0100

Hi,

On Mon, Jan 29, 2007 at 06:20:53PM -0500, Romanowski, Tim wrote:
> Right now, I am simply appending startDate.toString() to my query string,
> which I understand is bound to fail without some parsing or formatting, but
> I'm wondering what the appropriate way to do this is. Here is a sample
> query:
>
> SELECT e FROM MyEntity e WHERE e.startDate > 'Sun Jan 28 19:00:00 EST 2007'

Have you tried this: let's say you have a variable called oldestValidDate which
is a Date object and has a value that represents 'Sun Jan 28 19:00:00 EST 2007'.
Create a NamedQuery with the query

@NamedQuery(name="SelectNewerThanDate" query="SELECT e FROM MyEntity e WHERE e.startDate > :checkDate");

and call the query with the parameter oldestValidate. No formattng, no
converting to String or whatever, just the date object:

@PersistenceContext EntityManager em;
Query query = em.createNamedQuery("SelectNewerThanDate");
query.setParameter("checkDate", oldestValidDate);

By the way, EQL knows of three date functions:

CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP

The datetime functions return the value of current date, time, and timestamp on
the database server.

So this should also work

@NamedQuery(name="SelectNewerThanTwoDaysAgo" query="SELECT e FROM MyEntity e WHERE e.startDate > CURRENT_DATE-2");


HTH, Wouter van Reeven
>
>
>
> The Entity code looks like this:
>
>
>
> @Column(name = "STARTDATE")
>
> @Temporal(TemporalType.TIMESTAMP)
>
> private Date startDate;
>
>
>
> @Column(name = "STOPDATE")
>
> @Temporal(TemporalType.TIMESTAMP)
>
> private Date stopDate;
>
>
>
>
>
> Is there an easy way to store precise timestamps into the database using
> Glassfish' EJBQL dialect? An easy way to construct queries using them? If
> not easy, I'll settle for _any_ way =) I'm also interested in how to
> handle timezones (such as with oracle's TIMESTAMP WITH TIMEZONE column
> type), but won't be greedy.
>
>
>
>
>
> Tim
>


-- 
People: "If she weighs the same as a Duck, she's made of wood!"
Sir Bedevere: "And therefore...?"