Java JPA 查询
Java JPA Queries
我正在尝试 select 数据库中特定日期早于 7 天的所有实体。它通过 SQLyog 工作正常,但在 Java 中它总是抛出这个错误:
[33, 76] The expression is not a valid conditional expression.
[76, 101] The query contains a malformed ending.
这是我在 Java 中的查询:
SELECT a FROM Applicants a WHERE (a.lastMod <= CURRENT_DATE - INTERVAL 7 DAY) ORDER BY a.applDate ASC
问题可能出在 "CURRENT_DATE" 部分吗?
CURRENT_DATE
可以,但是 INTERVAL 7 DAY
不是有效的 JPQL 表达式。您需要提供日期作为参数
WHERE a.lastMod <= :dateParam
示例:
Query q = em.createQuery("SELECT a FROM Applicants a WHERE a.lastMod <= :dateParam ORDER BY a.applDate ASC");
q.setParameter("dateParam", dateParam);
List<Applicants> applicants = (List<Applicants>)q.getResultList();
// or, to avoid casting (thanks to @DavidSN)
TypedQuery<Applicants> q = em.createQuery("SELECT a FROM Applicants a WHERE a.lastMod <= :dateParam ORDER BY a.applDate ASC", Applicants.class);
q.setParameter("dateParam", dateParam);
List<Applicants> applicants = q.getResultList();
EntityManager em = ...
Query q = em.createQuery ("SELECT a FROM Applicants a WHERE a.lastMod <= :dateParam");
q.setParameter("dateParam" , dateParam);
List<blabla> results = q.getResultList ();
我正在尝试 select 数据库中特定日期早于 7 天的所有实体。它通过 SQLyog 工作正常,但在 Java 中它总是抛出这个错误:
[33, 76] The expression is not a valid conditional expression.
[76, 101] The query contains a malformed ending.
这是我在 Java 中的查询:
SELECT a FROM Applicants a WHERE (a.lastMod <= CURRENT_DATE - INTERVAL 7 DAY) ORDER BY a.applDate ASC
问题可能出在 "CURRENT_DATE" 部分吗?
CURRENT_DATE
可以,但是 INTERVAL 7 DAY
不是有效的 JPQL 表达式。您需要提供日期作为参数
WHERE a.lastMod <= :dateParam
示例:
Query q = em.createQuery("SELECT a FROM Applicants a WHERE a.lastMod <= :dateParam ORDER BY a.applDate ASC");
q.setParameter("dateParam", dateParam);
List<Applicants> applicants = (List<Applicants>)q.getResultList();
// or, to avoid casting (thanks to @DavidSN)
TypedQuery<Applicants> q = em.createQuery("SELECT a FROM Applicants a WHERE a.lastMod <= :dateParam ORDER BY a.applDate ASC", Applicants.class);
q.setParameter("dateParam", dateParam);
List<Applicants> applicants = q.getResultList();
EntityManager em = ...
Query q = em.createQuery ("SELECT a FROM Applicants a WHERE a.lastMod <= :dateParam");
q.setParameter("dateParam" , dateParam);
List<blabla> results = q.getResultList ();