Search code examples
hibernatehql

Hibernate - HQL parameters passing


I try to implement that (MySQL) using hibernate to create a report containing amount of notes per trade for a given day. I think that's pretty clear and selfdescriptive:

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;

So far I have this. "%" is a wildcard. :

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));

Unfortunately I get:

unexpected token: ':creationDate%'

Encapsulating with {} doesn't work. What is a proper way of opening and closing property id?


Solution

  • Put the "%" in the parameter, not in the query string.

    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) + "%");