If you are using JPA or its implementation you should be able to annotate your beans with:
@Version
private Long version;
So every time you persist you bean the version number will be increased by one automatically. In case you bean has lower/different version number during persisting it means it was changed in meantime and JPA throws Optimistic locking exception.