Code  fails with . I am just trying to pesimistically lock an entity in the same transaction where I created it.
The cause is:
1) Entity has SEQUENCE id generator, Hibernate doesn't flush before the find() nor before the lock().
2) With PESSIMISTIC_WRITE Hibernate has to issue SELECT FOR UPDATE.
Because entity is not inserted at the time of the SELECT FOR UPDATE, the find/lock fails....
Currently I workaround this by flushing before acquiring the pessimistic lock.