You seem to be very confused, the correct way to update objects in JPA is to read the "object" and modify it through its "set" methods, not use a "update" JPQL query.
Update queries in JPQL are for performing mass updates for batch processing, not to be used for updating objects.
Optimistic locking ensures that the object was not changed on the database while it was read into the EntityManager persistence context, but you are changing it directly on the database using your update query.
The lock is also not required, as the lock is always checked when you update the object.
# public void udateRole() {
# Role role;
# try {
# role = em.find(Role.class, "1");
# role.setRoleName("3");
# em.flush(); // not required, but will trigger any exception early.
# } catch (Exception e) {
# System.out.println(e);
# }
# }
If for some odd reason your want to use a raw query, not your object model, then
you should remove the em.lock() to avoid lock contention with yourself.