Skip to main content

[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 Tom,

Thanks for the information. Indeed, when I execute the query once (calling getResultList()) I can get the SQL string :

findAllBooksCriteriaQuery.getResultList();
System.out.println("SQL   > " + findAllBooksCriteriaQuery.unwrap(EJBQueryImpl.class).getDatabaseQuery().getSQLString());

But how do you prepare it ? I've tried the following but it doesn't work :

findAllBooksCriteriaQuery.unwrap(EJBQueryImpl.class).getDatabaseQuery().prepareForExecution();

The only method prepareCall I found needs some parameter : prepareCall(Session session, Record translationRow)

Thanks
Antonio

2012/5/22 Tom Ware <tom.ware@xxxxxxxxxx>
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
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



--
--
Antonio Goncalves (antonio.goncalves@xxxxxxxxx)
Software architect

Web site : www.antoniogoncalves.org
Blog: agoncal.wordpress.com
Feed: feeds2.feedburner.com/AntonioGoncalves
Paris JUG leader : www.parisjug.org
LinkedIn: www.linkedin.com/in/agoncal

Back to the top