Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of using CURRENT_DATE in JPA query

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 it is specified in JPA all providers should comply with it right?

I'm using EclipseLink 2.0 BTW.

like image 410
javydreamercsw Avatar asked Oct 28 '09 13:10

javydreamercsw


2 Answers

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

like image 106
McDowell Avatar answered Oct 05 '22 11:10

McDowell


The answer to this question was retrieving the values via JPA and then do the math in plain Java.

like image 29
javydreamercsw Avatar answered Oct 05 '22 11:10

javydreamercsw