【发布时间】:2012-05-29 12:42:25
【问题描述】:
我需要查询 5 天以前的东西。(自定义日期) 我看过 HQL,但由于它是持久性的,我无法根据here访问 setTimestamp@
String hqlQuery;
Calendar minDate = Calendar.getInstance();
minDate.add(Calendar.DATE, -days);
hqlQuery = "select n from notifications where n.app_id=:app and ((n.sent_date<=:minDate) OR (n.sent_date is null)) and n.handled='N'";
我就是这样尝试的... 任何甜蜜的帮手,在此先感谢:-)
编辑: 所以我改变了我的方法,它现在看起来像这样:
public static List<Notification> findOlderThen(EntityManager em, Long app, int days) {
String hqlQuery;
Calendar minDate = Calendar.getInstance();
minDate.add(Calendar.DATE, -days);
hqlQuery = "select n from notifications where n.app_id=:app and ((n.sent_date<=:minDate) OR (n.sent_date is null)) and n.handled='N'";
System.out.println(hqlQuery);
return em.createQuery(hqlQuery).setParameter("app", app).setParameter("minDate",minDate.getTime()).getResultList();
}
但它给出了这个错误:
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [select n from notifications where n.app_id=:app and ((n.sent_date<=:minDate) OR (n.sent_date is null)) and n.handled='N'], line 1, column 28: syntax error at [where].
Internal Exception: UnwantedTokenException(found=where, expected 80)
【问题讨论】:
-
所以....当您尝试此操作时遇到异常?
-
不能试,不能给查询加日期,至少不知道怎么弄。
-
你需要声明一个别名。
select n from notifications n where...
标签: java hibernate persistence hql