Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPQL to SQL converter

Is it possible to access the SQL which is generated by JPQL?

I would like to use the SQL to later create a view for my purposes.

I am using Hibernate if it matters.

like image 856
JavaRocky Avatar asked Mar 02 '10 23:03

JavaRocky


2 Answers

Is it possible to access the SQL which is generated by JPQL?

You can access the SQL String from an Hibernate Interceptor, more precisely from the method Interceptor.html#onPrepareStatement(java.lang.String)

onPrepareStatement

String onPrepareStatement(String sql) Called when sql string is being prepared. Parameters: sql - sql to be prepared Returns: original or modified sql

If you decide to go this way, the best option is to extends EmptyInterceptor and to override only the methods you want.

You can hook your interceptor using the following declaration in your persistence.xml:

<properties>
  <property name="hibernate.ejb.interceptor" value="com.acme.MyInterceptor"/>
</properties>
like image 147
Pascal Thivent Avatar answered Sep 30 '22 04:09

Pascal Thivent


You can set the hibernate.show_sql property to true, then all the SQL will be shown on the console.

<property name="hibernate.show_sql">true</property>
like image 24
beny23 Avatar answered Sep 30 '22 04:09

beny23