JPQL to SQL Converter

Is it possible to access the SQL generated by JPQL?

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

I use Hibernate if that matters.

+4
source share
3 answers

So, you need to write your own JPQL implementation, and then :-) After all, converting JPQL to SQL for RDBMS data storage is a JPQL implementation. Good luck

Of course, you could use an existing JPQL implementation, be it Hibernate or EclipseLink, or DataNucleus ... and since they are all open source, you have to go and delve into your code base.

+1
source

Is it possible to access the SQL generated by JPQL?

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

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 route, the best option would be to extend the EmptyInterceptor and override only those methods that you want.

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

 <properties> <property name="hibernate.ejb.interceptor" value="com.acme.MyInterceptor"/> </properties> 
+6
source

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

 <property name="hibernate.show_sql">true</property> 
+3
source

Source: https://habr.com/ru/post/1302930/


All Articles