I understand what is optimistic locking and "how it work" but I don't know how can I implement it on Java EE.
I have an entity in JPA and I add one more version column and I annotated it with @Version
. But to have an optimistic locking management I need only @Version annotation?
This is my Java Class:
@Entity
public class MyClass implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
//other variables
@Version
@Column(name = "version")
private int version;
//other code
}
In my project I use Stateless session bean to access to Entity class and persist more operations, so I use @TransactionManagement(TransactionManagementType.CONTAINER)
by default to handle my transaction.
My doubt is:
with @TransactionManagement(TransactionManagementType.CONTAINER)
( independently what is @TransactionAttribute
(REQUIRED,MANDATORY etc)) and only annotating version
variable in MyClass.java I obtain the optimistic locking management?