How to call CriteriaBuilder.lessThan with parameter of Object type

Viewed 801

In the deprecated Criteria API I can do something like this:

Object value = myFunc(); //this function returns numeric value based on some criteria
criteria.add(Restrictions.lt("column", value));

The important functionality for me is to not know the type at compile time, this is determined in runtime. In the new CriteriaQuery API when I try to call the below code it gives me an error.

Object value = myFunc(); //this function returns numeric value based on some criteria
CriteriaBuilder builder = session.getCriteriaBuilder();
criteriaQuery.where(builder.lessThan(root.get("column"), value));

Cannot resolve method 'lessThanOrEqualTo(javax.persistence.criteria.Path, java.lang.Object)'

1 Answers

So just cast the value to Comparable as the API expects it.

Related