If the purpose of you locking is to prevent double INSERT, JPA is already doing it for you. You can just use .save() as below and it will do an update if the object has already been persisted.
repository.save(object);