-
Notifications
You must be signed in to change notification settings - Fork 46
Description
Expected behavior
As a tester, I want to modify some data from the server multiple times and check if it is correctly modified so that I have created an endpoint that retrieves the data I am modifying.
Actual behavior
After the first modification I can not modify that Entity again unless the new data contains the same modificationCounter as the data in the DB
Steps to reproduce (bug) / Use Case of feature request (enhancement)
-
Executing a POST request to create a new visitor on JumpTheQueue server.
-
Check the ID of the new Entity (I saved it as a postman variable)
The name is the field that we are changing.
After that if we try to modify it again we recive the following internal server error (500) from postman:
And in the server side the console says:
Caused by: org.springframework.orm.ObjectOptimisticLockingFailureException: Object of class [com.devonfw.application.jtqj.visitormanagement.dataaccess.api.VisitorEntity] with identifier [1000320]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.devonfw.application.jtqj.visitormanagement.dataaccess.api.VisitorEntity#1000320]
The code the server is executing during the update is the following:
@Override
public VisitorEto updateVisitor(VisitorEto visitor) {
Objects.requireNonNull(visitor, "Visitor");
VisitorEntity visitorEntity = getBeanMapper().map(visitor, VisitorEntity.class);
VisitorEntity resultEntity = getVisitorRepository().save(visitorEntity);
LOG.debug("Visitor with id '{}' has been updated.", resultEntity.getId());
return getBeanMapper().map(resultEntity, VisitorEto.class);
}
Comments/Hints:
If we try to update the Entity data using the current modificationCounter field of the Entity we can update it. To do that the server code has been changed to:
@Override
public VisitorEto updateVisitor(VisitorEto visitor) {
Objects.requireNonNull(visitor, "Visitor");
VisitorEntity visitorEntity = getBeanMapper().map(visitor, VisitorEntity.class);
Optional<VisitorEntity> actualEntity = getVisitorRepository().findById(visitorEntity.getId());
if (actualEntity.isPresent()) {
visitorEntity.setModificationCounter(actualEntity.get().getModificationCounter());
}
VisitorEntity resultEntity = getVisitorRepository().save(visitorEntity);
LOG.debug("Visitor with id '{}' has been updated.", resultEntity.getId());
return getBeanMapper().map(this.resultEntity, VisitorEto.class);
}
Affected version:
- Devon 3.2.4
- OS: Windows 10
- Postman