Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Query from Hibernate saveOrUpdate(Object) (not for logging)

Tags:

java

hibernate

There is provision in Hibernate that you can get query from Criteria How to get SQL from Hibernate Criteria API (*not* for logging) but I want to get update/delete query from Hibernate saveOrUpdate and delete(Object) so is there any option?

like image 777
Vicky Thakor Avatar asked Feb 11 '15 09:02

Vicky Thakor


1 Answers

There is no such option as I am aware of. The update / save (insert) / delete methods are issued lazly. It was one of the main features to consider Hibernate back in the early days a decade ago.

Hibernate issues update/save/delete actions only if a select hits the database, a flush is issued or a commit happens. Then Hibernate walks all Objects (and their graphs) to check for dirty objects. Out of the dirtyness it generates SQL necessary and try to create batches of the changes. This makes it possible to save Connection time and transmit many changes at once. It can drastically speed up the database access if you have to delete /insert / update a lot of rows within the same table.

But this makes it impossible to receive the Hibernate generated SQL from such an action since the action does not exist yet it is just a marker within the POJO object.


If you want the SQL you can simply do two things. First of all you can replace the Driver implementation and insert any kind of logging to it (just as a monitor tool would do). Just grab the source code of your current Driver implementation and create a class + package within your own classpath and modify the methods like execute + setParameter. This way you can see and make it programmatically available what SQL hits the database and also you can replace the result set implementation and check how often your code issues next and all sorts of stuff.

I used it for unit testing and it worked well.

-

The second method is more easy. You just step into the source code of Hibernate and see it for yourself. If Garvin (King) has one special trait then it is writing good readable code. There are only some rare occasions you can argue with him about the way he writes (ok for me the methods are a bit too long) but beside this very good read. So if you can get the hands on Hibernates source code (which is easy) it is worth the read. I consumed the sources for one week I guess for Hibernate 3 and it was a pleasure (Eclipse is also a good read).

Once you check the code you will see that there are Classes representing the SQL before it is composed and that there are Dialect objects and everything. Investigate them, copy source provide your own implementation based on the original version and yet again you can monitor store and access and even manipulate all the things that results in the final SQL and even grab the SQL before it is handed over to the database driver.

-

The first option is good for seeing all SQL. The second option is very good for gaining a feeling about Hibernate and increase the control you have about what it actually does.

like image 80
Martin Kersten Avatar answered Sep 19 '22 14:09

Martin Kersten