【问题标题】:HQL currentdateHQL 当前日期
【发布时间】:2019-05-04 19:36:11
【问题描述】:

我正在尝试使用 currentdate() 函数在 HQL 中创建查询来比较两个日期。

List<Rel_Utenti_Commesse> commesseAttive = new ArrayList<Rel_Utenti_Commesse>();        

commesseAttive = session.createQuery("from Rel_Utenti_Commesse where utenti = '" + utenteSessione + "' and data_fine_commessa > current_date() or data_fine_commessa is null").list();

for(int i = 0; i < commesseAttive.size(); i++)                   
    System.out.println(commesseAttive.get(i).getCommessa().getNome_commessa());

return commesseAttive;

但是,尽管我在数据库(MySQL)中有一个带有 2019-02-01 的 data_fine_commessa 的实例,但他忽略了它。

为什么?

【问题讨论】:

    标签: mysql hibernate hql


    【解决方案1】:

    你错过了or部分的大括号:

    commesseAttive = session.createQuery("from Rel_Utenti_Commesse where utenti = '" + utenteSessione + "' and (data_fine_commessa > current_date() or data_fine_commessa is null)").list();
    

    这就是为什么首先评估 AND 而不是 OR

    @TimBiegeleisen 建议,使用参数化查询:

    Query query = session.createQuery("from Rel_Utenti_Commesse where utenti = :utenteSessione and (data_fine_commessa > current_date() or data_fine_commessa is null)")
    query.setParameter("utenteSessione", utenteSessione)
    List result = query.list()
    

    【讨论】:

    • 不知道 HQL 是否可行,但准备好的语句在这里可能会有所帮助。
    • @TimBiegeleisen 为什么在 HQL 中不能使用大括号?
    • 不,我不是指那个。我指的是 HQL 查询字符串是通过连接构建的。这对于普通的 SQL 来说会很糟糕……不确定 HQL。无论如何都要为你 +1。
    • @TimBiegeleisen 好吧。对不起,我的误解。是的,可以在 HQL 中使用占位符
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2017-01-02
    相关资源
    最近更新 更多