Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Created February 9, 2017 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thjanssen/6bc1df67975ae93bff90b5cd73136a48 to your computer and use it in GitHub Desktop.
Save thjanssen/6bc1df67975ae93bff90b5cd73136a48 to your computer and use it in GitHub Desktop.
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Book.class);
Root root = cq.from(Book.class);
// call the database function calculate
ParameterExpression doubleParam1 = cb.parameter(Double.class);
ParameterExpression doubleParam2 = cb.parameter(Double.class);
cq.where(cb.greaterThan(doubleParam2, cb.function(“calculate”, Double.class, root.get(Book_.price), doubleParam1)));
TypedQuery q = em.createQuery(cq);
q.setParameter(doubleParam1, 10.0D);
q.setParameter(doubleParam2, 40.0D);
List books = q.getResultList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment