我尝试使用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的正确方法?
答案 0 :(得分:1)
把"%"在参数中,而不是在查询字符串中。
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) + "%");