How to do a timestamp comparison with JPA query?

Posted by Robert on Stack Overflow See other posts from Stack Overflow or by Robert
Published on 2010-03-29T15:15:56Z Indexed on 2010/04/01 2:43 UTC
Read the original article Hit count: 759

Filed under:
|
|

We need to make sure only results within the last 30 days are returned for a JPQL query. An example follows:

Date now = new Date();
Timestamp thirtyDaysAgo = new Timestamp(now.getTime() - 86400000*30);

Query query = em.createQuery(
  "SELECT msg FROM Message msg "+
  "WHERE msg.targetTime < CURRENT_TIMESTAMP AND msg.targetTime > {ts, '"+thirtyDaysAgo+"'}");
List result = query.getResultList();

Here is the error we receive:

<openjpa-1.2.3-SNAPSHOT-r422266:907835 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: An error occurred while parsing the query filter 'SELECT msg FROM BroadcastMessage msg WHERE msg.targetTime < CURRENT_TIMESTAMP AND msg.targetTime > {ts, '2010-04-18 04:15:37.827'}'. Error message: org.apache.openjpa.kernel.jpql.TokenMgrError: Lexical error at line 1, column 217.  Encountered: "{" (123), after : ""

Help!

© Stack Overflow or respective owner

Related posts about jpql

Related posts about jpa