vendredi 23 janvier 2015

Hibernate optimistic locking test

I know this has been asked before, but I'm trying to test Hibernate's optimistic locking feature using a version field.


I've seen the following answer which outlines how to test this Hibernate 3 : optimistic locking unit test.


Can someone confirm whether or not my test is doing this correctly as I'm not getting the StaleObjectStateException that I'm expecting to see.



@Test(expected = StaleObjectStateException.class)
public void optimisticLockingTest() {
DummyEntity entity= new DummyEntity("name");
entityDao.persist(entity);

entityDao.detach(entity);
entity.setName("new name");

// Get new instance of the same entity
final DummyEntity newEntity = entityDao.findById(entity.getId());
newEntity.setName("Different name");
entityDao.persist(newEntity);

entity = entityDao.merge(entity );
entityDao.persist(entity);
}

Aucun commentaire:

Enregistrer un commentaire