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

There is a condition in Hibernate that you can get a request from Criteria How to get SQL from the Hibernate APIs (* not * for logging) , but I want to receive a request to update / delete from Hibernate saveOrUpdate and delete(Object) , is there any option?

+6
source share
3 answers

There is no such option that I know of. Ways to update / save (insert) / delete are lazy. This was one of the main features that Hibernate considered back in the early decades.

Sleep mode only updates / saves / deletes actions if the choice falls into the database, a flash is issued, or a commit occurs. Hibernate then scans all objects (and their graphs) to check for dirty objects. Due to the dirtyness, it generates an SQL query and tries to create change lots. This allows you to save connection time and transfer several changes at once. This can significantly speed up access to the database if you need to delete / insert / update many rows in one table.

But this makes it impossible to get the induced SQL Hibernate from such an action, because the action does not exist yet, but it is just a marker in the POJO.


If you want SQL, you can just do two things. First of all, you can replace the driver implementation and insert any kind of logging into it (as a monitor tool would do). Just take the source code of your current implementation of the Driver and create a class package + in your own classpath and change methods such as execute + setParameter. Thus, you can see and make programmatically accessible that SQL gets into the database, and you can also replace the implementation of the result set and check how often your code calls the following and all kinds of things.

I used it for unit testing and it worked well.

-

The second way is simpler. You just go into the Hibernate source code and see it for yourself. If Garvin (King) has one feature, then he writes good readable code. There are only rare cases where you can argue with him about how he writes (good for me, the methods are too long), but next to this very good reading. Therefore, if you can get the source code for Hibernates (which is easy), it is worth reading. I consumed the sources for one week, I think, for Hibernate 3, and that was nice (Eclipse also reads well).

Once you check the code, you will see that there are classes representing SQL before it is created, and that there are Dialect objects and all that. Examine them, the copy source will provide your own implementation based on the original version, and again you can control access to the repository and access to them and even manage all the things that lead to the final SQL, and even capture SQL before it is passed to the driver Database.

-

The first option is good for viewing all of SQL. The second option is very good for getting a sense of sleep mode and increasing control over what it actually does.

+1
source

As you already mentioned, you can get the generated SQL query execution time from criteria or from QueryDSL or from named queries (for example, using QueryTranslator ), but, unfortunately, it is simply impossible to do for the built-in methods such as save (), update ( ) etc.

The only way to get ( to see , to be more precise) generated SQL is with logging or database monitoring tools.

Although this is one of the most popular features, it still is not part of the specification, so it is strictly dependent on the implementation of vendors (and desires). However, Hibernate provides no way to get this, at least at this point.

0
source

If you want to customize UPDATE / DELETE statements, you can simply provide your own CRUD DML statements:

 @Entity @Table(name="CHAOS") @SQLInsert( sql="INSERT INTO CHAOS(size, name, nickname, id) VALUES(?,upper(?),?,?)") @SQLUpdate( sql="UPDATE CHAOS SET size = ?, name = upper(?), nickname = ? WHERE id = ?") @SQLDelete( sql="DELETE CHAOS WHERE id = ?") @SQLDeleteAll( sql="DELETE CHAOS") 
0
source

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


All Articles