5

Is it possible to mix JPQL and CriteriaQuery?

I have a String that contains a where condition in JPQL and a CriteriaQuery that is already built and got from somewhere else. I wonder if I can add the where condition String to CriteriaQuery.where(). The where condition could be multiple conditions "and-ed"/"or-ed" together.

Can I do this without parsing the where condition String?

Tom Tucker
  • 11,676
  • 22
  • 89
  • 130

3 Answers3

2

As noted before, there is no common specification for this in JPA (that I know of). In implementations, however, there may be methods to convert queries into expressions or restrictions. In TopLink (and presumably in EclipseLink) Expression.postfixSQL and prefixSQL are the methods that come closest. Hibernate has Restrictions.sqlRestriction.

I don't know about any other implementations.

mcyalcin
  • 2,074
  • 1
  • 15
  • 12
1

If there's a way to do this in JPA 1.0 or 2.0, I'm not aware of it, but this answer in another question suggests that mixing JPQL and CriteriaQuery would be available in JPA 2.1 (some time in the future).

Community
  • 1
  • 1
esaj
  • 15,875
  • 5
  • 38
  • 52
1

Well one thing you can do is to convert the CriteriaQuery to JPQL and concatenate the two WHERE parts of the strings.

See "JPQL equivalent of the Criteria query" on http://www.datanucleus.org/products/accessplatform/jpa/jpql_criteria.html

Drew
  • 1,332
  • 3
  • 15
  • 21