Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPQL query convert to SQL query

Tags:

java

sql

orm

jpa

jpql

Is it possible to convert JPQL query to SQL and save this query to database?

I want to write JPQL queries when developing program. But whet will deploy application to server I need compile querys to native SQL depends on DB.

like image 573
Oleksandr Samsonov Avatar asked Mar 04 '26 03:03

Oleksandr Samsonov


2 Answers

I found the answer for Hibernate HQL queries;

final Query query = sessionFactory.getCurrentSession().createQuery(hql);

final QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
final QueryTranslatorImpl newQueryTranslator = (QueryTranslatorImpl) ast.createQueryTranslator(queryId, query.getQueryString(), Collections.EMPTY_MAP, (SessionFactoryImplementor) sessionFactory);
newQueryTranslator.compile(null, false);
sql = newQueryTranslator.getSQLString();

Thanks to:

http://good-old-mushroom-called-bedla.blogspot.com/2012/04/how-to-convert-hql-to-sql-in-hibernate.html

like image 124
Oleksandr Samsonov Avatar answered Mar 05 '26 17:03

Oleksandr Samsonov


There will be no JPA standard way to get the SQL. The EclipseLink specific solution is available in EclipseLink's FAQ http://wiki.eclipse.org/EclipseLink/FAQ/JPA#How_to_get_the_SQL_for_a_Query.3F

like image 25
Chris Avatar answered Mar 05 '26 16:03

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!