0

I need add something like Restrictions.sqlRestriction to CriteriaQuery. How to add SQL condition to CriteriaQuery?

My code:

CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Installation> criteriaQuery = criteriaBuilder.createQuery(Test.class);
Root<Installation> root = criteriaQuery.from(Test.class);
criteriaQuery.select(root);
Akvel
  • 924
  • 1
  • 15
  • 32

3 Answers3

2

JPA Criteria allows function() to be used, or other clauses that have JPQL equivalents. Since you don't say what is the "SQL condition" then that's all that can be said. You certainly can't dump some random SQL into a JPQL filter for example

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
0

I am not sure that it can be done. I suggest you these 3 options:

  • Implement whatever you need to do in your Hibernate entity, but I guess you have already tried and are here looking for some other solution
  • Rework your entire query as SQL
  • A more creative one: define a new Hibernate entity on a database view doing the 'delicate' part of your query, and leave the rest to be managed via Criteria.
Jorge_B
  • 9,712
  • 2
  • 17
  • 22
0

Solution for my case (call Oracle function 'bitand')

   criteriaQuery.where(criteriaBuilder.notEqual(criteriaBuilder.function("bitand", Long.class, root.get(Test_.status).get(Status_.id), criteriaBuilder.parameter(Long.class, "status")),0));
   TypedQuery<Installation> query = em.createQuery(criteriaQuery.select(root));
   query.setParameter("status", 1L);
Akvel
  • 924
  • 1
  • 15
  • 32