0

I'm migrating some code that uses DetachedCriteria of Hibernate to a new one using standard CriteriaBuilder of JPA 2.1.

I have the following code :

criteria.add(Restrictions.sqlRestriction("sin(?) * sin(x) + cos(?) * cos(y) * cos(x- ?) <= ?", value, types));

I want to migrate it using CriteriaBuilder but i didn't find a method in the JPA 2.1 Criteria API to use custom JPQL.

Is there any way to do it ?


PS: I'm using Specification of Spring data JPA. So in my code I have access to CriteriaQuery and CriteriaBuilder.

public Predicate toPredicate(Root<Address> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

....
}
Radouane ROUFID
  • 10,595
  • 9
  • 42
  • 80
  • Define "custom JPQL". JPQL is well defined by the JPA spec, so if something is JPQL then it is in the spec, by definition. If instead you mean SQL functions then JPQL (and Criteria) has FUNCTION. – Neil Stockton Dec 24 '16 at 16:59
  • In my criteriaBuilder I want my entity to satisfy the following `sin(?) * sin(x) + cos(?) * cos(y) * cos(x- ?) <= ?` where x and y are two attributs of my entity. Does JPA Criteria API give a equivalent to `Restrictions.sqlRestriction` ? – Radouane ROUFID Dec 24 '16 at 17:11
  • It has an equivalent of invoking an SQL function .... as I already said ... "FUNCTION". And that is all you are doing there – Neil Stockton Dec 24 '16 at 17:11
  • Function is to execute just one database function. I want to execute multiple ones. Is that possible ? Thank you for your patience ! – Radouane ROUFID Dec 24 '16 at 17:34
  • so chain multiple! – Neil Stockton Dec 24 '16 at 18:40

0 Answers0