将CURRENT_TIMESTAMP()用于查询时出错

时间:2015-07-31 09:38:57

标签: jpa oracle10g timestamp jpql

我想使用当前日期函数

比较jpql查询中的两个日期

我收到了错误

  

语法错误解析[SELECT d FROM Dossier d WHERE d.depid = 1 AND d.typeDossier =:tpd AND d.dateCreation< CURRENT_TIMESTAMP()+ 5]。
  [105,107]左表达式不是算术表达式

这是我的疑问:

public List<Dossier> getDossierFindAllParDepartementDBTECHandUrgen() {
    return (List<Dossier>) em.createQuery("SELECT d FROM Dossier d WHERE d.depid=1 AND d.typeDossier = :tpd AND " +
        "d.dateCreation < CURRENT_TIMESTAMP() + 5",
                                          Dossier.class).setParameter("tpd", "Urgent").getResultList();
}

1 个答案:

答案 0 :(得分:1)

JPA支持函数CURRENT_TIMESTAMP

https://en.wikibooks.org/wiki/Java_Persistence/JPQL#Functions

但不适用于算术运算。您可以使用参数解决问题,例如

TypedQuery<Dossier> query = em.createQuery("SELECT d FROM Dossier d WHERE d.depid=1 AND d.typeDossier = :tpd AND " +
        "d.dateCreation < :fiveDaysAhead",
                                          Dossier.class);
Date myFiveDaysAhead = new Date(Calendar.getInstance().add(Calendar.DAYS_OF_YEAR,5).getTimeInMillis());//or something
query.setParameter("tpd", "Urgent");
query.setParameter("fiveDaysAhead", myFiveDaysAhead, TemporalType.TIMESTAMP);

也可能找到供应商特定的解决方案,正如我在另一个答案中注意到的https://stackoverflow.com/a/18514326/2835455