Skip to content

feat(core): fix duplicate issue with auditing#95

Open
RVANDO12 wants to merge 5 commits into
mainfrom
feat/audit/fix-duplicate
Open

feat(core): fix duplicate issue with auditing#95
RVANDO12 wants to merge 5 commits into
mainfrom
feat/audit/fix-duplicate

Conversation

@RVANDO12

Copy link
Copy Markdown
Collaborator

PR Description

Issue when batch update of relation between entity.

" Caused by: java.sql.BatchUpdateException: Batch entry 1 insert into idp_core.relation_target_entities_aud "
" ERROR: duplicate key value violates unique constraint "relation_target_entities_aud_pkey"

Fixes

Database: Expand the relation_target_entities_aud_pkey to include target_entity_uuid.

Code: Strip targetEntityUuid out of the equals() and hashCode() methods in RelationTargetJpaEntity.java so that the Java Set correctly prevents duplicates before they ever reach Hibernate Envers.

Review

The reviewer must double-check these points:

  • The reviewer has tested the feature
  • The reviewer has reviewed the implementation of the feature
  • The documentation has been updated
  • The feature implementation respects the Technical Doc / ADR previously produced
  • The Pull Request title has a ! after the type/scope to identify the breaking
    change in the release note and ensure we will release a major version.

How to test

Update a huge information about entity to cause the issue and check is not appears

@github-code-quality

github-code-quality Bot commented Jul 13, 2026

Copy link
Copy Markdown

Code Coverage Overview

Languages: Java

Java / code-coverage/jacoco

The overall coverage in the branch is 91%. The coverage in the branch is 90%.

Show a code coverage summary of the most impacted files.
File 5d25202 7891043 +/-
com/decathlon/i...enceMapper.java 73% 99% +26%

Updated July 15, 2026 12:27 UTC
Code Coverage is in Public Preview. Learn more and provide us with your feedback.

EntityJpaEntity jpa = mapper.toJpa(domain);

assertEquals(2, jpa.getProperties().size());
assertTrue(jpa.getProperties() instanceof Set);
@sonarqubecloud

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to address duplicate-key failures in Envers auditing during batch updates of entity relations by reducing unnecessary relation updates during merge operations, and by extending/strengthening test coverage around entity update payloads and persistence mapping.

Changes:

  • Added relation change detection in EntityPersistenceMapper.mergeRelations(...) to avoid updating unchanged relations (reducing unnecessary Envers audit writes).
  • Refactored EntityControllerTest request payloads to use JSON files under src/test/resources/... and added new update-with-relations scenarios.
  • Added a comprehensive EntityPersistenceMapperTest suite covering domain↔JPA mapping and merge behavior.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/main/java/com/decathlon/idp_core/infrastructure/adapters/persistence/mapper/EntityPersistenceMapper.java Adds relation change detection during merge to avoid unnecessary updates/audit writes.
src/test/java/com/decathlon/idp_core/infrastructure/adapters/api/controller/EntityControllerTest.java Switches multiple payloads to JSON fixtures; adds PUT relation-merge test scenarios.
src/test/java/com/decathlon/idp_core/infrastructure/adapters/persistence/mapper/EntityPersistenceMapperTest.java Adds extensive unit tests for persistence mapping and merge logic (including relation change detection).
src/test/resources/integration_test/json/entity/v1/putEntity_400_required_properties_missing.json New JSON fixture for PUT validation error scenario.
src/test/resources/integration_test/json/entity/v1/putEntity_400_property_type_mismatch.json New JSON fixture for PUT validation error scenario.
src/test/resources/integration_test/json/entity/v1/putEntity_400_property_rules_not_respected.json New JSON fixture for PUT validation error scenario.
src/test/resources/integration_test/json/entity/v1/putEntity_400_property_not_defined_and_relation_target_missing.json New JSON fixture for PUT combined validation error scenario.
src/test/resources/integration_test/json/entity/v1/putEntity_200_valid_update.json New JSON fixture for a valid PUT update.
src/test/resources/integration_test/json/entity/v1/putEntity_200_update_with_add_relation.json New JSON fixture for PUT update adding a relation.
src/test/resources/integration_test/json/entity/v1/putEntity_200_update_remove_all_relations.json New JSON fixture for PUT update removing all relations.
src/test/resources/integration_test/json/entity/v1/postEntity_400_required_properties_missing.json New JSON fixture for POST validation error scenario.
src/test/resources/integration_test/json/entity/v1/postEntity_400_relation_target_entity_does_not_exist.json New JSON fixture for POST validation error scenario.
src/test/resources/integration_test/json/entity/v1/postEntity_400_property_type_mismatch.json New JSON fixture for POST validation error scenario.
src/test/resources/integration_test/json/entity/v1/postEntity_400_property_rules_not_respected.json New JSON fixture for POST validation error scenario.
src/test/resources/integration_test/json/entity/v1/postEntity_400_property_not_defined_in_template.json New JSON fixture for POST validation error scenario.

Comment on lines +173 to +177
if (existingRel.getTargetEntities().size() != newTargetEntities.size()) {
return true;
}

return !existingRel.getTargetEntities().equals(newTargetEntities);
Comment on lines +148 to +152
if (hasRelationChanged(existingRel, domainRel)) {
existingRel.setTargetTemplateIdentifier(domainRel.targetTemplateIdentifier());
existingRel.setTargetEntities(domainRel.targetEntityIdentifiers() != null
? resolveBatchTargetEntities(domainRel.targetTemplateIdentifier(),
domainRel.targetEntityIdentifiers())
Comment on lines +137 to +139
/// **Optimization:** Change detection prevents false positives where Hibernate
/// marks collections as modified even though their values haven't changed,
/// which would cause Envers to create duplicate audit table entries.
Comment on lines +593 to +596
EntityJpaEntity existing = EntityJpaEntity.builder().id(existingId)
.templateIdentifier("template").name("name").id(existingId).templateIdentifier("template")
.name("name").identifier("id").properties(new HashSet<>())
.relations(new HashSet<>(Set.of(rel1))).build();
Comment on lines +707 to +708
@DisplayName("Should preserve relation IDs during merge when relation unchanged")
void putEntity_200_preserves_relation_ids_on_merge() throws Exception {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants