@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;
}
Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.
Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.
Top comments (0)