@Test public void testOptimistic() { String name = "optimistic"; long id = createUser(name); EntityManager em = factory.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); User u1 = em.find(User.class, id, LockModeType.OPTIMISTIC); assertNotNull(u1); TypedQuery<Long> query = em.createQuery("select count(u) from User u where u.name = ?1", Long.class); query.setParameter(1, name); long res = query.getSingleResult(); assertEquals(1, res); TransactionExecutor executor = new TransactionExecutor(factory); executor.syncExe(s -> { User user = s.find(User.class, id); user.setName("foo"); }); res = query.getSingleResult(); assertEquals(0, res); assertThrows(RollbackException.class, () -> { tx.commit(); }); em.close(); }
public void setName(String name) { this.name = name; }
User { public void setName(String name) { this.name = name; } }
User { public void setName(String name) { this.name = name; } User(); }
User { public void setName(String name) { this.name = name; } User(); Long getId(); void setId(Long id); String getName(); void setName(String name); Integer getVersion(); void setVersion(Integer version); }
User { public void setName(String name) { this.name = name; } User(); Long getId(); void setId(Long id); String getName(); void setName(String name); Integer getVersion(); void setVersion(Integer version);  }