我正在尝试从某个日期超过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();

关于java - Java JPA查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28673348/

10-12 01:29