In using Hibernate4 recently, I found two very mysterious annotations @DynamicInsert and @DynamicUpdate
If it is in the configuration file That is, dynamic-insert and dynamic-update
These two annotations can increase the speed related to database operations to a certain extent, save the execution time of SQL statements, and improve the operating efficiency of the program.
To use these two annotations, you only need to add them to the entity class, or configure them in *.hbm.xml. These two annotations are boolean values, true or false.
1. First use false to see the results of the execution, let’s take the update as an example:
a. The data in the database looks like this:< /p>
b, the code of the entity class
p>
C、 Test code:
After executing the test method, you can see:
< /p>
I only updated the Description attribute, but I updated the attributes of the entire object, which affected efficiency to a certain extent. And it may not be the result we want. The result we want is which fields I changed
Just update the fields I modified. Next, we change @DynamicUpdate(false) to @DynamicUpdate(true) to test Click, and the results are as follows
It’s magical. This is the moment to witness the miracle. We have achieved our goal and only update the fields we have modified. @DynamicInsert I will not give an example.
After the test, you can know:
In Hibernate, you can use @DynamicInsert and @DynamicUpdate to generate dynamic SQL statements, that is, when inserting and modifying data, the statement only includes the insertion or Modified fields.
Of course, there are other ways to achieve this effect, such as using the merge method provided by the session, which is also possible.