2
votes

I'm using Spring Data JPA + QueryDSL. I create my dynamic queries like this:

JPAQuery<Foo> query = jpaQueryFactory.select(...);

I have found this old article that shows how to retrieve programatically the native sql string: https://antoniogoncalves.org/2012/05/24/how-to-get-the-jpqlsql-string-from-a-criteriaquery-in-jpa/ but it doesn't work for me.

I have tried this:

String queryString1 = query.createQuery().unwrap(org.hibernate.query.Query.class).getQueryString();
String queryString2 = query.createQuery().unwrap(org.eclipse.persistence.jpa.JpaQuery.class).getDatabaseQuery().getSQLString();

The first doesn't returns me the the native sql but the JPQL string and the second fails to unrwap org.hibernate.query.internal.QueryImpl to org.eclipse.persistence.jpa.JpaQuery.

PS: I've tested before and after fetching the query.

1

1 Answers

0
votes

If you need native SQL query generated by Querydsl, then you need to use SQLQueryFactory instead JPAQueryFactory. Your JPQL query returned by JPAQueryFactory is transformed to SQL by JPA not by Querydsl.