Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating multiple times one Entity on DB #50

Open
JSGitHubbing opened this issue Jan 30, 2020 · 0 comments
Open

Updating multiple times one Entity on DB #50

JSGitHubbing opened this issue Jan 30, 2020 · 0 comments
Labels

Comments

@JSGitHubbing
Copy link

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)

  1. Executing a POST request to create a new visitor on JumpTheQueue server.
    image

  2. Check the ID of the new Entity (I saved it as a postman variable)
    image

  3. Executing a PUT request in order to modify the data.
    image

The name is the field that we are changing.
image

After that if we try to modify it again we recive the following internal server error (500) from postman:
image
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant