Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to retrieve JPQL query from Query object #25

Open
lukasj opened this issue May 24, 2012 · 9 comments
Open

Add support to retrieve JPQL query from Query object #25

lukasj opened this issue May 24, 2012 · 9 comments

Comments

@lukasj
Copy link
Contributor

lukasj commented May 24, 2012

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();
}
@lukasj
Copy link
Contributor Author

lukasj commented May 24, 2012

@glassfishrobot Commented
Reported by c.beikov

@lukasj
Copy link
Contributor Author

lukasj commented May 25, 2012

@glassfishrobot Commented
agoncal said:
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();

}

@lukasj
Copy link
Contributor Author

lukasj commented Feb 22, 2013

@glassfishrobot Commented
@ldemichiel said:
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

@lukasj
Copy link
Contributor Author

lukasj commented Jun 22, 2015

@glassfishrobot Commented
@arjantijms said:
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.

@lukasj
Copy link
Contributor Author

lukasj commented Jun 24, 2015

@glassfishrobot Commented
neilstockton said:
+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.

@lukasj
Copy link
Contributor Author

lukasj commented Jun 25, 2015

@glassfishrobot Commented
@arjantijms said:
I wrote an article about this issue and described how to do this when using Hibernate. See http://arjan-tijms.omnifaces.org/2012/06/counting-rows-returned-from-jpa-query.html

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.

@lukasj
Copy link
Contributor Author

lukasj commented May 5, 2017

@glassfishrobot Commented
This issue was imported from java.net JIRA JPA_SPEC-25

@lukasj
Copy link
Contributor Author

lukasj commented Aug 31, 2018

@beikov
Copy link

beikov commented Mar 18, 2022

FWIW, returning the JPQL/HQL string could be easily implemented in Hibernate 6.0 as the AST model provides the option to render to an HQL string. If anyone is willing to work on this, Hibernate can serve as a compatible implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants