In this case there is a column on the table that is nominated as the "Version" column. This matches the JPA @Version annotation. This is typically either a Counter, a Timestamp or a DBMS specific column such as Oracle "ora_rowscn" or MSSQLServer "RowVersion/Timestamp".
In performing the Optimistic Concurrency checking with a Version Column the UPDATE or DELETE statement has a WHERE clause that will include the primary key and the Version Column.
update b_product set name='Apple', sort_order=4, updtime=*NOW*
where id=1 and updtime=*PREVIOUS updtime*
The number of rows this effected is checked and if this was 0 then a OptimisticLockException is thrown. It is implied that the value of updtime has been changed by someone else since we fetched the data.
If the number of rows effected was 1 then the update was deemed successful.
Note: Because the primary key is part of the WHERE clause we should safely expect either 0 or 1 rows updated. It is assumed the primary key does not change.
Note: Users are not allowed to change the values of Version columns themselves.