Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java持久化中的旧查询_Java_Hibernate_Persistence_Hql - Fatal编程技术网

java持久化中的旧查询

java持久化中的旧查询,java,hibernate,persistence,hql,Java,Hibernate,Persistence,Hql,我需要做一个查询,在那里我可以查询超过5天的内容。(自定义日期) 我已经看过HQL,但因为它是持久性的,所以我没有访问setTimestamp的权限 字符串查询; Calendar minDate=Calendar.getInstance(); minDate.add(Calendar.DATE,-days); hqlQuery=“从通知中选择n,其中n.app\u id=:app和((n.sent\u date)如果我没有弄错,在查询中添加日期时,您不应该这样做吗: hqlQuery = "s

我需要做一个查询,在那里我可以查询超过5天的内容。(自定义日期) 我已经看过HQL,但因为它是持久性的,所以我没有访问setTimestamp的权限

字符串查询;
Calendar minDate=Calendar.getInstance();
minDate.add(Calendar.DATE,-days);

hqlQuery=“从通知中选择n,其中n.app\u id=:app和((n.sent\u date)如果我没有弄错,在查询中添加日期时,您不应该这样做吗:

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'";
hqlQuery=“从通知中选择n,其中n.app\u id=:app和((n.sent\u日期
假设发送日期的类型是Notifications model类中的日期,则设置日期参数的方式。此外,在查询的这一部分中,请确保Notifications是类的实际名称。大小写很重要!它可能应该是
select n from Notifications n

更新:


您需要声明类的别名。

因此…尝试此操作时出现异常?无法尝试,无法将日期添加到查询,至少不知道如何声明。您需要声明别名。
从通知中选择n,其中…
您不必这样做,hibernate应自动识别发送日期是一个日期,并且为你逃避价值。
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)
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'";
SessionFactory sf = // get your session factory
Query q = sf.getCurrentSession()
        .createQuery(hqlQuery);
q.setParameter("minDate",minDate.getTime())
 .setParameter("app", appId)
 .list();