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.