Junior — Middle
Have you used the Version property in Hibernate for version control when creating objects?
sobes.tech AI
Answer from AI
In Hibernate, the @Version property is used to implement an optimistic locking mechanism, which helps control object versions during updates in the database. This prevents data loss when the same record is modified concurrently by multiple transactions.
Example usage:
@Entity
public class Product {
@Id
private Long id;
private String name;
@Version
private int version;
// getters and setters
}
With each update, Hibernate automatically increments the version field. If the version in the database does not match the object's version in memory during save, an OptimisticLockException is thrown, indicating a conflict in updates.