代码之家  ›  专栏  ›  技术社区  ›  aerion

Hibernate-HQL参数传递

  •  0
  • aerion  · 技术社区  · 7 年前

    我尝试使用hibernate实现这一点(MySQL),以创建一个包含给定日期每笔交易的票据数量的报告。我认为这是非常清楚和自我描述的:

    select customer.trade, COUNT(*) from customer join note where customer.customer_id=note.customer_id and note.CREATION_TS like '${DATE}%' group by customer.trade;
    

    到目前为止,我有这个。“%”是通配符:

    Query query = session.createQuery(
                        "select new ReportEntry(c.trade, count(*)) from customer as c join note as n where c.id=n.customer.id and n.creationDate like ':creationDate%' group by c.trade;")
                        .setParameter("creationDate", DATE_FORMAT.format(reportDate));
    

    不幸的是,我得到:

    unexpected token: ':creationDate%'
    

    用{}封装不起作用。打开和关闭属性id的正确方法是什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Paul Hicks    7 年前

    Query query = session.createQuery(
       "select new ReportEntry(c.trade, count(*)) "
       + "from customer as c join note as n "
       + "where c.id=n.customer.id "
       + "  and n.creationDate ':creationDate' group by c.trade;")
      .setParameter("creationDate", DATE_FORMAT.format(reportDate) + "%");