Software Developer
Spring Data JPA provides a framework that works together with JPA and provides a complete abstraction over the Data Access Layer in a project. Spring Data JPA brings in a concept of JPA Repositories a set of Interfaces that defines query methods.
When we use spring information JPA with hibernate, we will use the extra functions of hibernating as well. @Dynamicupdate is one such function. @Dynamicupdate is a category-level annotation that may be implemented to a JPA entity. It ensures that hibernate makes use of most effective the modified columns inside the square declaration that it generates for the update of an entity.
While software starts, hibernate generates the square statements for crud operations of all of the entities. This sq. Statements are generated once and are cached, in reminiscence, to enhance the performance.
The generated sq. Update statement includes all of the columns of an entity. In case we update an entity, the values of the modified columns are exceeded to the sq. Update announcement. For the columns that aren't updated, hibernate uses their current values for the replacement.
Let's try to understand this with an example. First, let's consider a JPA entity named Account:
@Entity
public class Account {
@Id
private int id;
@Column
private String name;
@Column
private String type;
@Column
private boolean active;
// Getters and Setters
}
Next, let's write a JPA repository for the Account entity:
@Repository
public interface AccountRepository extends JpaRepository<Account, Integer> {
}
Now, we'll use the AccountRepository to update the name field of an Account object:
Account account = accountRepository.findOne(ACCOUNT_ID);
account.setName("Test Account");
accountRepository.save(account);
After we execute this update, we can verify the generated SQL statement. The generated SQL statement will include all the columns of Account:
update Account set active=?, name=?, type=? where id=?
We've seen that even though we've modified the name field only, Hibernate has included all the columns in the SQL statement.
Now, let's add the @DynamicUpdate annotation to the Account entity:
@Entity
@DynamicUpdate
public class Account {
// Existing data and methods
}
Next, let's run the same update code we used in the previous section. We can see that the SQL generated by Hibernate, in this case, includes only the name column:
update Account set name=? where id=?
So, what happens when we use @DynamicUpdate on an entity?
When we use @dynamicupdate on an entity, hibernate does now not use the cached sq. Statement for the replacement. Alternatively, it's going to generate a square announcement every time we replace the entity. This generated square consists of the handiest of the modified columns.
To discover the changed columns, hibernate wishes to song the kingdom of the contemporary entity. So, while we trade any field of an entity, it compares the contemporary and the changed states of the entity.
This means @dynamicupdate has an overall performance overhead related to it. Consequently, we must best use it whilst it's required.
Truly, there are a few eventualities in which we have to use this annotation — as an example if an entity represents a table that has a big wide variety of columns and just a few of these columns are required to be updated frequently. Also, while we use model-much less optimistic locking, we want to use @dynamicupdate.
In this tutorial, we have regarded the @DynamicUpdate annotation of hibernating. We have used an example of spring information JPA to peer @dynamicupdate in movement. Also, we have mentioned while we have to use this feature and whilst we must not.