Look at the evidence


Word, the first official API – doc @ DynamicUpdate out (pictured), see https://docs.jboss.org/hibernate/stable/orm/javadocs/

  • For updates, whether the entity should reference the changed column only in a preprocessed SQL statement when it is generated using dynamic SQL.
  • Note that for reattaching detached entities, this cannot be done if select-before-update is not enabled.

For chestnuts


First, the @dynamicUpdate annotation is used on entity classes

There is the database table tBL_foo and the corresponding entity class Foo as follows:

id name col3 col4 col5
1 Picasso 1770 Austria male
class Foo{
    private Integer id;
    private String name;
    private String col3;
    private String col4;
    private String col5;
    getter...
    setter...
}
Copy the code

Update the name attribute of the record id=1 in the Service as follows:

Foo foo = fooDao.findById(1);
foo.setName("Beethoven");
fooDao.save(foo);
Copy the code

There are two cases:

  1. The @dynamicUpdate annotation is not used in the Entity class or @dynamicUpdate (false) is used in the Entity class.
update tbl_foo set name=? , col3=? , col4=? , col5=?where id=?
Copy the code
  1. The @dynamicUpdate annotation (or @dynamicUpdate (true)) is used in the Entity class, and the underlying SQL executed by Hibernate looks like:
update tbl_foo set name=? where id=?
Copy the code

The results for database updates are equivalent in both cases, but using @dynamicUpdate gives better performance. Because when @dynamicUpdate is not used, even unchanged fields are updated. If updates are frequent and only a few fields are updated at a time, then @DynamicUpdate is a good performance optimization.

misunderstanding


We often have a requirement that the Web layer receives the value of the property to be modified by the front end with an object (the value that is not modified is null), and we want to call the DAO’s Update method directly for selective updates.

The Web layer uses objects to receive property values to be modified by the front end, which is equivalent to executing the following code:

Foo foo = new Foo();
foo.setId(1);
foo.setName("Beethoven");
Copy the code

The service layer then uses this object directly for updates

fooDao.update(foo);
Copy the code

What we expect:

id name col3 col4 col5
1 Beethoven 1770 Austria male

If we add @dynamicUpdate to the entity class, we can do this.

Instead, the result is:

id name col3 col4 col5
1 Beethoven

Once you fully understand the api-doc instructions for @dynamicupdat, it’s easy to see why you get this result. DynamicUpdate compares the value of the field in the entity class to be used for the update with the value of the field queried from the database to determine whether it has changed. In this example, all fields in the database with id=1 are non-null, but only name has a value in the entity class. That is, all fields have changed, but other fields have been updated with new null values.

Gossip (some nonsense can be ignored)


  • The easiest, most reliable, and most direct way to learn how to use an annotation (or class), whether open source or commercial, is through first-hand official API documentation.

  • Some people use @dynamicUpdate when using Spring Data JPA and assume it is the same as Spring, but it is not. In fact, it is obvious from the package name when the annotation is referenced that Hibernate is the original. The reason of course is that the ORM implementation used by Spring Data JPA is Hibernate. This explains that the documentation in the figure above is Hibernate API documentation, not Spring or Java API documentation.

The above are all personal views, hope to help you. My level is limited, unavoidably have wrong or fallacious place, hope everybody points out. Any other questions are welcome to communicate with you!