[JPA_SPEC-25] Add support to retrieve JPQL query from Query object Created: 24/May/12  Updated: 25/Jun/15

Status: Open
Project: jpa-spec
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major
Reporter: c.beikov Assignee: ldemichiel
Resolution: Unresolved Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Tags: JPA_next, jpql, query

 Description   

Currently the interfaces javax.persistence.criteria.AbstractQuery and javax.persistence.Query have no support for retrieving the underlying JPQL Query for additional transformation which would be, especially for named queries, very useful! Just think of the fact, that you currently have to copy and paste a named query if you like to have exactly the same query with an additional predicate in the where clause. The predicate in the where clause is just an example, there are probably tons of use cases where this feature would be helpful and minimize redundant code!

I would suggest to add the following methods:

Query.java
public interface Query{
  // currently available methods...

  public String getQuery();
}
AbstractQuery.java
public interface AbstractQuery{
  // currently available methods...

  public String getQuery();
}


 Comments   
Comment by Antonio Goncalves [ 25/May/12 ]

As I wrote on my blog (http://agoncal.wordpress.com/2012/05/24/how-to-get-the-jpqlsql-string-from-a-criteriaquery-in-jpa/), having the JPQL String representation of a CriteriaQuery is very useful for debugging purpose. And going further, having the SQL String would also be useful. I would go for :

public interface Query {

  public String getJPQLQuery();
  public String getSQLQuery();

}
Comment by ldemichiel [ 22/Feb/13 ]

Being able to map back and forth between CriteriaQueries and JPQL queries would be useful to have. This is something that should be considered in a future release. See also http://java.net/jira/browse/JPA_SPEC-29

Comment by arjan tijms [ 22/Jun/15 ]

Just wondering if this is still considered, specifically for Java EE 8 (for which no JPA EG has been formed as of now).

In the 3 years since this issue has been created the lack of this feature impedes my work on almost a daily basis. In particular it makes paging/sorting from services quite difficult and obscure.

Comment by neilstockton [ 24/Jun/15 ]

+1 but I would suggest that the method to return the (SQL) native query is actually

String getNativeQuery();

which then aligns with how "SQL" is accessible in the query API.

Comment by arjan tijms [ 25/Jun/15 ]

I wrote an article about this issue and described how to do this when using Hibernate. See http://jdevelopment.nl/counting-rows-returned-jpa-query

Ideally something like would be standardized. Note that just String getNativeQuery() is not really enough. Some info about the translated query parameters is also needed.

Generated at Sat Jan 21 00:24:15 UTC 2017 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.