Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the JPA generated SQL query?

I use JPA specification and Hibernate as my vendor. I need somehow to take the generated SQL Query which is sent to the the DB (printed to the sysout) and save it as a simple string.

Is there a way to do this?

EDIT

Let me make it a beat clearer: I don't need hibernate log. I need to be able to execute the same query on a different DB. Therefore, I need to get the SQL query as is, and hold it in a normal String variable.

Edit 2

Is there a util which I can provide it a bean and it will automatically generate an Insert query? can I somehow use Hibernate beans here? I know it's a beat complex.

Thanks,

Idob

like image 902
Ido Barash Avatar asked Jan 16 '13 12:01

Ido Barash


People also ask

How can we see Hibernate generated SQL on console?

Hibernate has build-in a function to enable the logging of all the generated SQL statements to the console. You can enable it by add a “show_sql” property in the Hibernate configuration file “ hibernate. cfg. xml “.

Can we write SQL query in JPA?

We can create a SQL query with the @Query annotation by following these steps: Add a query method to our repository interface. Annotate the query method with the @Query annotation, and specify the invoked query by setting it as the value of the @Query annotation's value attribute.


4 Answers

Create a bean like this.

@Bean
public JpaVendorAdapter jpaVendorAdapter(){
    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setGenerateDdl(true);
    jpaVendorAdapter.setShowSql(true);

    return jpaVendorAdapter;
}

If you're using Spring Boot add it somewhere to your @Configuration.

The logs created from this are executable in MySQL workbench. You stated that you are using JPA and Hibernate. There's no other way except if the database you support are supported by JPA. In that case there is an AbstractJpaVendorAdapter that you can implement.

like image 189
Jade Devin Nocum Cabatlao Avatar answered Oct 22 '22 12:10

Jade Devin Nocum Cabatlao


The simple answer to your question is No. What you want to do is something that many developers would also like to do however it was not part of the JPA specification and thus the ability to get the generated SQL will depend upon what the vendor decided to do. Using Hibernate the only way to obtain the SQL is via the log.

like image 39
Scott Gustafson Avatar answered Oct 22 '22 12:10

Scott Gustafson


You have to enable the log4j logging and add an appender for Hibernate to show the queries.

This has already been described here: How to print a query string with parameter values when using Hibernate

like image 1
ioan Avatar answered Oct 22 '22 10:10

ioan


If I understand you correctly, you want to get the insert query which Hibernate is executed on one database, and via code, run it on a different database via entityManager#executeUpdate or similar.

Hibernate does not expose the generated query as it is specific for the dialect of target database. So even if were to get the insert query, it could be pointless.

However in your case, you can create two database connections (via two DataSource or EntityManagerFactory whatever in your case) and call dao.persist(entity) two times for both databases, and let Hibernate handle the query construction part.

Edit: By query I mean native query here, HQL query would be same for both databases. Hope it helps.

like image 1
Archit Avatar answered Oct 22 '22 12:10

Archit