Skip to content

Update using $inc does not work if @Version is present in the entity #4918

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

Closed
symphony-enrico opened this issue Mar 10, 2025 · 3 comments
Closed
Assignees
Labels
type: bug A general bug

Comments

@symphony-enrico
Copy link

symphony-enrico commented Mar 10, 2025

Given following entity:

@Document("entitlementJobExecution")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EntitlementJobExecution {
  @Id
  private ObjectId id;

  private Long executedLines;
  private Long successfullyExecutedLines;

  @Version
  private Long version;

}

and the following repository:

@Repository
public interface EntitlementJobExecutionRepository extends MongoRepository<EntitlementJobExecution, ObjectId> {
  @Update("{ '$inc' : { 'executedLines' : ?1, 'successfullyExecutedLines' : ?2 } }")
  void findAndIncrementExecutedLinesAndSuccessfullyExecutedLinesById(
    ObjectId id,
    int executedLines,
    int successfullyExecutedLines
  );
}

The query does not work as expected. No error is reported, but it generates:

o.s.data.mongodb.core.MongoTemplate      : Calling update using query: { "_id" : { "$oid" : "67cf20da7b8c784443a05eb5"}} and update: { "$inc" : { "version" : 1}} in collection: entitlementJobExecution

Thus, executedLines and successfullyExecutedLines are not updated.
If I remove @Version from entity, it works as expected.

Thanks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 10, 2025
@symphony-enrico
Copy link
Author

For info, generating manually the update with MongoTemplate and criteria:

  public void incrementExecutedLinesAndSuccessfullyExecutedLinesById(
    ObjectId id,
    int executedLines,
    int successfullyExecutedLines
  ) {
    Query query = Query.query(Criteria.where("_id").is(id));
    Update update = new Update()
      .inc("executedLines", executedLines)
      .inc("successfullyExecutedLines", successfullyExecutedLines);
    mongoTemplate.updateFirst(query, update, EntitlementJobExecution.class);
  };

Works as expected:

o.s.data.mongodb.core.MongoTemplate      : Calling update using query: { "_id" : { "$oid" : "67d00a70a522b64eb464ddb6"}} and update: { "$inc" : { "executedLines" : 1, "successfullyExecutedLines" : 0, "version" : 1}} in collection: entitlementJobExecution

So this problem concern only MongoRepository

@christophstrobl
Copy link
Member

Thank you for reporting - we'll look into it.

@christophstrobl christophstrobl self-assigned this Mar 11, 2025
@christophstrobl
Copy link
Member

The issue revealed a general problem with BasicUpdate and how certain operations are handled there. Working on a fix.

@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 11, 2025
mp911de pushed a commit that referenced this issue Mar 12, 2025
This change makes sure basic update appends values to operations instead of overriding them. This change aligns the behaviour with Update and fixes issues where using the Update annotation with versioned entities can lead to loss of update information.

Closes: #4918
Original pull request: #4921
mp911de added a commit that referenced this issue Mar 12, 2025
Merge List values in Update. Add tests, reformat code.

See: #4918
Original pull request: #4921
mp911de pushed a commit that referenced this issue Mar 12, 2025
This change makes sure basic update appends values to operations instead of overriding them. This change aligns the behaviour with Update and fixes issues where using the Update annotation with versioned entities can lead to loss of update information.

Closes: #4918
Original pull request: #4921
mp911de added a commit that referenced this issue Mar 12, 2025
Merge List values in Update. Add tests, reformat code.

See: #4918
Original pull request: #4921
mp911de added a commit that referenced this issue Mar 12, 2025
Merge List values in Update. Add tests, reformat code.

See: #4918
Original pull request: #4921
@mp911de mp911de added this to the 4.3.10 (2024.0.10) milestone Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants