Hibernate - HQL 参数传递
Hibernate - HQL parameters passing
我尝试使用休眠来实现 (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 的正确方法是什么?
将“%”放在参数中,而不是查询字符串中。
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) + "%");
我尝试使用休眠来实现 (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 的正确方法是什么?
将“%”放在参数中,而不是查询字符串中。
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) + "%");