I'm writing a regular JEE8 application with JPA with @Version (OptimisticLock). My DAO is a CDI bean marked with @Transactional(value = TxType.REQUIRED)
when an update is conflicting: org.hibernate.StaleObjectStateException should be wrapped in javax.persistence.OptimisticLockException but that's not the case.
I receive a javax.transaction.RollbackException and the cause is org.hibernate.StaleObjectStateException.
How such thing is possible ?
Were is the expected javax.persistence.OptimisticLockException ? It appears nowhere (in all exception causes chain).
tested on Wildfly 10 and 14: hibernate 5.0.10 and 5.3.6
What I'm doing is updating an Actor with a child table ActorProperties. Actor is the one with @Version with TimeStamp. I do something like that:
public Supplier<Long> updateActorOptimiticLock(Actor actor) {
Map<String,Object> hints = new HashMap<String,Object>();
hints.put("javax.persistence.fetchgraph", em.getEntityGraph("actor with accesses and their props"));
ActorEntity currentEntity = em.find(ActorEntity.class, actor.getId(),hints);
if (currentEntity.getLastUpdate().getTime()!=actor.getLastUpdate())
{
throw new OptimisticLockException("Dto obsolete");
}
ActorEntity detachedActorEntity = mt.fromDto(actor);
em.merge(detachedActorEntity);
em.lock(currentEntity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
return ()->currentEntity.getLastUpdate().getTime();
}
I force the increment because properties don't have @Version in my model.
Also My DAO looks like this:
@Transactional(value = TxType.REQUIRED)
public class ServiceDAO {
@PersistenceContext
protected EntityManager em;
}