When to use @Version and @Audited in Hibernate?

Can someone help me if using when to use @Version and @Audited in Hibernate?

+21
source share
2 answers

@Version is used to implement Optimistic locking with Hibernate, which means that no two transactions override data at the same time as the conflict.
If the data is read by two streams at the same time, and both try to update the same line with different values, Hibernate uses the @Version field to check if the line is updated.
Before committing, each transaction verifies that no other transaction has changed its data. When you change the last transaction, the "Work with obsolete data" error occurs.

@Audited used to perform an audit function on Hiberate Envers objects

+31
source

@Version - used to implement optimistic locking, see 2.2.1.2. Version control for optimistic locking . Optimistic locking is useful when you do not expect many concurrent records and do not want to pay for locking the database.

@Audited - comes from the Envers API and can be used to automatically track changes to entities in a separate audit table. Use Envers to keep track of changes in some of your entities.

+13
source

All Articles