[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Cannot get the JPQL & SQL String of a CriteriaQuery

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 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);
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) ?