[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [eclipselink-users] Cannot get the JPQL & SQL String of a CriteriaQuery
|
Hi Antonio,
EclipseLink will not translate a Criteria query into JPQL for you. The
getJPQLString() method will only work for queries originally written in JPQL.
The getSQLString() method relies on the query being "prepared" as indicated
in the comment for that method. One you have run the query once, it will be
prepared. Alternately, you should be able to call prepareCall, or prepareCalls
on it to cause it to prepare.
-Tom
On 21/05/2012 9:17 AM, Antonio Goncalves wrote:
Hi all,
I need to get the JPQL and/or SQL String of a type safe query... but I can't
make it work. Let's take a simple example : find all the Book entities. Here is
what I can do with a simple dynamic query :
TypedQuery<Book> findAllBooksQuery = em.createQuery("SELECT b FROM Book b ORDER
BY b.id <http://b.id> DESC", Book.class);
System.out.println("JPQL " +
findAllBooksQuery.unwrap(EJBQueryImpl.class).getDatabaseQuery().getJPQLString());
System.out.println("SQL " +
findAllBooksQuery.unwrap(EJBQueryImpl.class).getDatabaseQuery().getSQLString());
As you can see, I use the unwrap(EJBQueryImpl.class) method to get the needed
string. And this works fine. But if I do the following, I get a null (for both
JPQL and SQL String) :
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Book> q = cb.createQuery(Book.class);
Root<Book> b = q.from(Book.class);
q.select(b).orderBy(cb.desc(b.get("id")));
TypedQuery<Book> findAllBooksCriteriaQuery = em.createQuery(q);
System.out.println("JPQL " +
findAllBooksCriteriaQuery.unwrap(EJBQueryImpl.class).getDatabaseQuery().getJPQLString());
System.out.println("SQL " +
findAllBooksCriteriaQuery.unwrap(EJBQueryImpl.class).getDatabaseQuery().getSQLString());
Any idea what this happens ? I am using the right implementation (i.e.
EJBQueryImpl) ?
Thanks
Antonio
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users