2020-05-14

Hibernate JPA @DynamicUpdate Example

@DynamicUpdate

The @DynamicUpdate annotation 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.

For updating, should this entity use dynamic sql generation where only changed columns get referenced in the prepared sql statement?
Note, for re-attachment of detached entities this is not possible without select-before-update being enabled.

Dynamic updates

To enable dynamic updates, you need to annotate the entity with the @DynamicUpdate annotation:

Example : Product entity mapping

@Entity(name = "Product")
@DynamicUpdate
public static class Product {

@Id
private Long id;

@Column
private String name;

@Column
private String description;

@Column(name = "price_cents")
private Integer priceCents;

@Column
private Integer quantity;

//Getters and setters are omitted for brevity

}

No comments:

Post a Comment