Code such as the following will break with positional parameters:
Listwith the error message:states = widgetService.findSomeWidgetStates();
Query q = entityManager.createQuery("select count(w) from Widget w where w.state in (?)");
q.setParameter(1, states);
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field example.Widget.id to....But if you change to named parameters, as in:
Query q = entityManager.createQuery("select count(w) from Widget w where w.state in (:widgetStates)");Everything just works. So it seems named parameters are worth a little extra typing.
q.setParameter("widgetStates", states);
No comments:
Post a Comment