So I understand what setting this attribute dynamic-update=true does, i.e it considers only those fields that were modified and omits the job of setting null values for other fields. Saving an overhead, good on performance.
Asking out of curiosity: How does hibernate come to know what all fields were modified? Does it do a comparison of the result generated by the select query to the database first before firing an update query? Assuming yes, then isn't comparison an overhead to performance?
Correct me if I'm wrong. Thanks in advance!
@DynamicUpdate is a class-level annotation that can be applied to a JPA entity. It ensures that Hibernate uses only the modified columns in the SQL statement that it generates for the update of an entity.
When the dynamic-insert property is set to true , Hibernate does not include null values for properties (for properties that aren't set by the application) during an INSERT operation. With the dynamic-update property set to true, Hibernate does not include unmodified properties in the UPDATE operation.
Annotation Type DynamicUpdateSpecifies that SQL update statements for the annotated entity are generated dynamically, and only include columns which are actually being updated. This might result in improved performance if it is common to change only some of the attributes of the entity.
Annotation Type DynamicInsertSpecifies that SQL insert statements for the annotated entity are generated dynamically, and only include the columns to which a non-null value must be assigned. This might result in improved performance if an entity is likely to have many null attributes when it is first made persistent.
So after almost 2 month's wait I was finally able to derive this conclusion proposed as an answer to my own question with the help from various sources:
1.) When using dynamic-update
, Hibernate has to generate the corresponding SQL string each time and there is thus a performance cost on the Hibernate side. In other words, there is a trade-off between overhead on the database side and on the Hibernate side.
2.) Hibernate caches the actual SELECT, INSERT and UPDATE SQL strings for each entity. This results in not having to re-create these statements every time you want to find, persist or update an entity.However, when using dynamic-update
, Hibernate has to generate the corresponding SQL strings each time. This results in a performance cost on the Hibernate side.
3.) Useful when applied to a very small & simple table as there is a significant performance gain using this annotation. A more realistic scenario, on wider tables using a remote database, performance increases may be more pronounced. Of course, the mileage you get out of this will vary most of the columns need to be updated.
4.) The @DynamicUpdate annotation/ dynamic-update=true
is used to specify that the UPDATE SQL statement should be generated whenever an entity is modified. By default, Hibernate uses a cached UPDATE statement that sets all table columns. When the entity is annotated with the @DynamicUpdate
annotation, the PreparedStatement is going to include only the columns whose values have been changed.
Overall could be summarized as:
Credits: Hibernate ORM 5.2.7.Final User Guide,http://memorynotfound.com/hibernate-dynamic-update-attriburte-example/ , https://stackoverflow.com/a/3405560/1004631
P.S.: Also holds true for dynamic-insert=true/ @DynamicInsert
annotation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With