filmov
tv
Spring Data JPA - How to Use Dynamic Inserts and Updates

Показать описание
When you persist a new entity or update an existing one with Spring Data JPA, you might have recognized that you’re always executing the same SQL statement that sets all columns mapped by the entity. That’s even the case if you only update one of its attributes.
That is a performance optimization provided by Hibernate, the JPA implementation that Spring Data JPA uses by default. Hibernate tries to avoid checking which attributes of an entity have changed and generating a specific SQL statement for them. It instead generates 1 SQL UPDATE and 1 SQL INSERT statement for each entity class at startup and reuses it for every insert or update operation.
Reusing the same statement over and over again improves Hibernate’s performance. But it also creates some side effects. These statements create an overhead if you only change 1 attribute of a huge entity class. They also cause problems if you need to audit all changes performed on a database table. In these cases, it might be better to let Hibernate generate a new SQL INSERT or UPDATE statement for each operation.
Like my channel? Subscribe!
Want to connect with me?
That is a performance optimization provided by Hibernate, the JPA implementation that Spring Data JPA uses by default. Hibernate tries to avoid checking which attributes of an entity have changed and generating a specific SQL statement for them. It instead generates 1 SQL UPDATE and 1 SQL INSERT statement for each entity class at startup and reuses it for every insert or update operation.
Reusing the same statement over and over again improves Hibernate’s performance. But it also creates some side effects. These statements create an overhead if you only change 1 attribute of a huge entity class. They also cause problems if you need to audit all changes performed on a database table. In these cases, it might be better to let Hibernate generate a new SQL INSERT or UPDATE statement for each operation.
Like my channel? Subscribe!
Want to connect with me?
Комментарии