Skip to content

Updating multiple times one Entity on DB #50

@JSGitHubbing

Description

@JSGitHubbing

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions