1

我需要做一个查询,我把 5 天以前的东西拿走。(自定义日期)我查看了 HQL,但由于它是持久性的,我无法按照这里访问 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)
4

2 回答 2

3
SessionFactory sf = // get your session factory
Query q = sf.getCurrentSession()
        .createQuery(hqlQuery);
q.setParameter("minDate",minDate.getTime())
 .setParameter("app", appId)
 .list();

假设 send_date 的类型是 Notifications 模型类中的 Date ,将是您设置日期参数的方式。此外,在查询的这一部分,请select n from notifications确保通知是您班级的实际名称。案件很重要!应该是select n from Notifications n

更新:

您需要为该类声明别名。

于 2012-05-29T12:51:00.760 回答
0

如果我没记错的话,当你在查询中添加日期时,你不应该做这样的事情吗:

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'";

注意 minDate 变量周围的 '

于 2012-05-29T12:49:58.490 回答