@version annotation allows Hibernate to activate the optimistic locking whenever executing an UPDATE and DELETE statements.
@Entity@Table(name = "orders")
public class Order {
@Id
private long id;
@Version
private int version;
private String description;
private String status;
}
Version-less Optimistic locking
But in this approach, it considers the entity as a whole unit.
If you change two fields at the same time, then It will throw an ObjectOptimisticLockingFailureException.
To handle this we can use simply version-less optimistic locking.
This can be achieved by using @OptimisticzLocking and @DynamicUpdate annotations.
@Entity
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
public class VersionlessProduct {
@Id
private UUID id;
private String name;
private int stock;
}
Top comments (0)