È necessario assicurarsi che vengano restituiti solo i risultati degli ultimi 30 giorni per una query JPQL. Segue un esempio:Come eseguire un confronto di data/ora con la query JPA?
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();
Qui è l'errore che riceviamo:
<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 : ""
Aiuto!
Che cos'è ts? Perché hai parentesi nella tua domanda? {...} – Rick
Tentativo di utilizzare un "indicatore di data e ora di sintassi di escape JDBC" come indicato nei seguenti collegamenti. Sembra che il suggerimento di DataNucleus sia una soluzione molto migliore. http://openjpa.apache.org/builds/latest/docs/manual/jpa_langref.html#jpa_langref_lit http://publib.boulder.ibm.com/infocenter/cscv/v10r1/topic/com.ibm.cloudscape.doc /rrefjdbc41784.html#rrefjdbc41784 – Lightbeard
Dovresti aprire un bug contro OpenJPA per ottenere il codice ... o il documento corretto. – Rick