Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting HQL to SQL query

I wanted to know how can we convert a HQL query into a sql query .I know if we make the showsql = true parameter on we can get the sql query but the parameter value would not be appended with its values .I need to find ways to print the SQL query generated in logs and use for my performance optimization .

Thanks in advance

like image 352
Sam Avatar asked Dec 02 '25 22:12

Sam


1 Answers

You can check my answer here: https://stackoverflow.com/a/37749916/1350643

In short you can use following code to convert hql to sql:

QueryTranslatorFactory translatorFactory = new ASTQueryTranslatorFactory();
SessionFactoryImplementor factory = (SessionFactoryImplementor) getSessionFactory();
QueryTranslator translator = translatorFactory.
        createQueryTranslator(hqlQueryText, hqlQueryText, Collections.EMPTY_MAP, factory);
translator.compile(Collections.EMPTY_MAP, false);
translator.getSQLString(); 

Source: http://narcanti.keyboardsamurais.de/hibernate-hql-to-sql-translation.html

like image 165
Alexander Avatar answered Dec 05 '25 13:12

Alexander



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!