When using the CriteriaQuery API and invoking a query using the uniqueResultOptional() methodthrows a QueryException for a Named parameter not set. This exception is not thrown if the JPA method getResultList() is used instead.
After debugging this I determined the root cause is the Query returned by Session.createQuery(CriteriaQuery) is a CriteriaQueryTypeQueryAdapter. It overrides the methods for getSingleResult() and getResultList() to invoke the corresponding methods on its jpqlQuery method, a fully populated QueryImpl. Hibernate-specific methods, including uniqueResultOptional() are not overridden by CriteriaQueryTypeQueryAdapter, and thus the corresponding methods are not invoked on the jpqlQuery member.
Here is a sample source that causes the problem:
And a stack trace snippet illustrating the issue: