views:

763

answers:

2

Can anyone point me to an example on how to use CURRENT_DATE in a JPA query?

CURRENT_DATE is specified in JPA but I haven't been able to make it work. I always get the unexpected token [CURRENT_DATE] exception. Since is specified in JPA all providers should comply with it right?

I'm using EclipseLink 2.0 BTW.

A: 

If you are using the Expression Framework there is the Expression currentDateDate() method on the Expression (org.eclipse.persistence.expressions.Expression) object.

jitter
I'll need to try it but I was really trying to avoid using generic JPA queries to avoid dependance on the persistence provider.
javydreamercsw
A: 

It can be used like so:

Query query = manager
    .createQuery("SELECT c FROM CITIES c WHERE c.founded = CURRENT_DATE");
for (Object city : query.getResultList()) {
  System.out.println(city);
}

...where founded is a temporal type:

  @Column(name = "FOUNDED")
  @Temporal(TemporalType.DATE)
  private Date founded = new Date();

Not a great example, but you get the idea. I'm using Eclipselink 1.1.2

McDowell
You're really close. I got that far just before seeing your reply. I'm trying to do something likeSelect x from Table x where x.id=1 and (x.lastModified-CURRENT_DATE)>=365I need to use calculation on dates.
javydreamercsw
Is it possible to do datetime arithmetic using JPA? You'd have to have some function to turn the difference into days (and not minutes or months or millennia, etc.). I am not aware of any such functions. Do the date arithmetic in Java and then just select comparing against the resultant range.
McDowell
I guess this is a flaw or missing functionality of JPA. I'll have to do it that way I guess. Thanks!
javydreamercsw