@DynamicUpdate with Spring Data JPA | An overview

@DynamicUpdate with Spring Data JPA | An overview
Techiio-author
Written by Sagar RabidasFebruary 24, 2022
7 min read
Hibernate
4 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 4 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we know @DynamicUpdate with Spring Data JPA

Introduction of Spring Data JPA:-

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.

JPA @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=?

JPA @Entity with @DynamicUpdate:-

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.

Conclusion:-

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.

Hibernate
Spring
JPA
DynamicUpdate
4 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 4 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+9 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance