Junior — Middle
Have you used @Version annotations in your projects for concurrency control?
sobes.tech AI
Answer from AI
Yes, the @Version annotation in JPA is used for implementing optimistic locking, which helps manage concurrent access to data. When updating an entity, the framework checks that the record's version in the database matches the version in memory, preventing overwriting changes made by other transactions.
Example of usage:
@Entity
public class Product {
@Id
private Long id;
@Version
private Integer version;
private String name;
private Double price;
// getters and setters
}
Attempting to update an entity with an outdated version will throw an OptimisticLockException, which allows for proper conflict handling.