Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug JPA CriteriaBuilder queries

How can I debug a query built with JPA 2.0 CriteriaBuilder? Is there a way to print out the query that is being executed?

I am developing a web application using NetBeans, MySql, GlassFish. I would avoid starting MySql in debug mode, because it is used also for other applications. JPA provider is EclipseLink.

like image 624
perissf Avatar asked Dec 29 '11 19:12

perissf


People also ask

How do I find query in CriteriaBuilder?

Create an instance of CriteriaBuilder by calling the getCriteriaBuilder() method. Create an instance of CriteriaQuery by calling the CriteriaBuilder createQuery() method. Create an instance of Query by calling the Session createQuery() method. Call the getResultList() method of the query object, which gives us the ...

What is the use of CriteriaBuilder?

Interface CriteriaBuilder. Used to construct criteria queries, compound selections, expressions, predicates, orderings. Note that Predicate is used instead of Expression<Boolean> in this API in order to work around the fact that Java generics are not compatible with varags.


1 Answers

The same attributes in persistence.xml that also print the SQL that is generated from regular JPQL queries, should also print the SQL that is generated from Criteria queries.

E.g.

For Hibernate (used by e.g. JBoss AS) it's:

<property name="hibernate.show_sql" value="true" />

For EclipseLink (used by e.g. GlassFish) it's:

<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>

Also see: How to view the SQL queries issued by JPA?

like image 155
Arjan Tijms Avatar answered Sep 17 '22 19:09

Arjan Tijms