2

Is it possible to transform a JPQL sting like select x from Clazz x into a CriteriaQuery object where I could check if the query's root is Clazz or not (using CriteriaQuery.getRoots) or do the same thing differently (not transforming into CriteriaQuery)?

The JPA docs are quite simple (e.g. CriteriaBuilder's class comment says "Return an instance of CriteriaBuilder for the creation of CriteriaQuery objects." - 10 or 20 sentences and references more wouldn't hurt). It thus doesn't mention anything about reversing the CriteriaQuery -> TypedQuery process.

jpa - transforming jpql join query to criteria api and How to convert a JPQL with subquery to Criteria API equivalent? cover concrete cases. I'm looking for a generic programmatic solution.

Specifying a type which doesn't match with the argument of CriteriaBuilder.createQuery(Class) causes an IllegalArgumentException, but that's more of a workaround and doesn't allow a very useful set of methods to investigate.

Community
  • 1
  • 1
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177

1 Answers1

3

No, not possible, or at least no standardised way.

See also this issue where someone has requested it for a future version of JPA.

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