Hibernate uses @Dynamicinsert and @Dynamicupdate to generate dynamic SQL statements

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>

share picture

b, the code of the entity class

p>

share picture

C、 Test code:

share picture

After executing the test method, you can see:

Share a picture< /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

Share pictures

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.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4471 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.