Skip to content

ValueObjects/Embeddables changes aren't correctly detected after committing #1372

Closed

Description

@Thessi

When deleting an embedded object from an entity and committing that entity, Javers doesn't pick up on those changes when querying for that data. The deleted fields in the embeddable aren't recognized or reflected as changes in any way. Other changes in the entity are picked up as TerminalValueChange.

A second case is deleting an entity containing an (initialized) embeddable. Again, all fields are emitted as a TerminalValueChange, but the fields inside the embeddable aren't mentioned in the changes.

If I instead diff the entities (Javers#compare()), I get all expected changes.

I'd be willing to look into a PR for this, but I'd first need a triage to check if this is an actual issue, and some pointers to where to start, since I'm not familiar with the code.

Steps To Reproduce
Runnable test case is here:

https://github.com/Thessi/javers/blob/master/javers-core/src/test/groovy/org/javers/core/cases/CaseRemoveValueObjectReference.groovy

I implemented tests for both cases, and showed that a diff of the objects correctly recognizes all changes, whereas going through the commit doesn't.

Javers' Version
7.4.2

Additional context
I found some workarounds for both cases (in case someone urgently needs this functionality):

For the first case a workaround would be initializing all embeddables with empty objects before calling Javers#commit(), so Javers explicitly picks up on every null field.

For the second case, the workaround is more difficult and depends on the type of deletion. If we use Javers#commitShallowDelete(), we can first set all embeddable fields in the entity to an empty object, and persist it using Javers#commit(). Then, we commit the delete. Note: This will split the deletion in two separate commits. Also this only works if the deleted entity is merged and up to date with the entityManager. Otherwise this might also trigger some other changes.

If instead javers#commitShallowDeleteById() is used, I currently don't see a consistent workaround.

Activity

  1. bartoszwalacik commented on May 22, 2024

    @bartoszwalacik
    Member

    Hi @Thessi , thanks for a high quality report, I will take a look on this

  2. bartoszwalacik commented on May 25, 2024

    @bartoszwalacik
    Member

    Hi @Thessi , you correctly identified the issues.

    The first case is a bug. A Value Object is a component of an owning Entity, so if a VO is nulled, it should be marked as deleted in Javers repo. Which means Javers should commit an explicit Terminal Snapshot for a nulled VO. Then, changes should be calculated correctly. Unfortunately, if we fix it this change won't be backward compatible, it will correct only new commits. Still, I believe it's important to fix it.

    The second case is more like a missing feature than a bug, as the docs says:

        /**
         * Marks given object as deleted.
         * <br/><br/>
         *
         * Unlike {@link Javers#commit(String, Object)}, this method is shallow
         * and affects only given object.
         * <br/><br/>
         *
         * This method doesn't delete anything from JaVers repository.
         * It just persists 'terminal snapshot' of a given object.
         *
         * @param deleted object to be marked as deleted (Entity or Value Object)
         */
        Commit commitShallowDelete(String author, Object deleted);
    

    Please note that VOs are committed in separate snapshots.
    But since there is no good workaround to perform a cascade delete, I agree that a new method would come in handy.
    We can name it commitCascadeDelete() --

        /**
         * Marks given and all its child Value Objects object as deleted.
         * <br/><br/>
         *  
         * This method doesn't delete anything from JaVers repository.
         * It just persists 'terminal snapshot' of a given object and its children.
         *
         * @param deleted object to be marked as deleted (Entity or Value Object)
         */
        Commit commitCascadeDelete(String author, Object deleted);
    

    Please let me know if you are eager to contribute a PR

  3. Thessi commented on May 29, 2024

    @Thessi
    Author

    @bartoszwalacik I'll try to work on a PR in the next few days. I'll start with the first case for now, I'll let you know once I got something working.

  4. bartoszwalacik commented on Jun 4, 2024

    @bartoszwalacik
    Member

    good luck @Thessi 👍

  5. olsavmic commented on Jan 15, 2025

    @olsavmic

    Hi @bartoszwalacik,
    we have arrived to the same bug mentioned in this issue (ValueObject removal is not visible in the generated diff), yet when I looked into the code, it seems like this was partly by design:

    org.javers.core.JaversCommitE2ETest#should not record ObjectRemoved for removed ValueObject

    Do you remember why? I'd like to work on the fix but this seems intentional. Thank you!

  6. bartoszwalacik commented on Jan 24, 2025

    @bartoszwalacik
    Member

    Good catch @olsavmic , indeed that test case stays in opposition. Still, seems that we all agree that this logic should be changed, so also this test should be aligned with the new logic.

  7. olsavmic commented on Jan 30, 2025

    @olsavmic

    I've created a draft PR with a fix for the TerminalValueChange on ValueObjects (#1430).

    I'd appreciate some guidance (@bartoszwalacik), as I slightly doubt that adding the TerminalValueChange for VO conceptually fits the approach towards VOs in Javers 🤔

  8. bartoszwalacik commented on Feb 19, 2025

    @bartoszwalacik
    Member

    Hi @olsavmic , I did some digging. You are right , that adding the TerminalValueChange for VO reference when this reference is unset is not the right direction. What we need is the set of TerminalValueChanges for all props of the unset VO.

    It will require a bit more coding. The problem lies in SnapshotDiffer.calculateDiffs(). Two facts are important:

    • Javers doesn't generate TerminalSnapshots fo VO
    • SnapshotDiffer composes the big diff from a list of atomic diffs when only two snapshots are compared -- the new version vs the old version.
    • since there is no TerminalSnapshot fo VO, we miss one atomic diff -- TerminalSnapshot VO vs Previous Snapshot VO

    So diff in findChanges() (which is based on SnapshotDiffer) works differently than diff in compare(), as in compare() only one big diff is calculated on the object graph level (no atomic one-to-one diffs!). And in this case, Javers can figure out which objects are removed from the new graph, and for them, this (virtual) Terminal vs Previous diff is calculated.

    I believe that the right approach to solve it is to change this list of atomic one-to-one diffs in
    SnapshotDiffer into more sophisticated approach :

    1. aggregate list of snpshots by Owner Entity Id & Commit Id, so into the list of sets of snapshots where each sets contains an Entity with all its child Value Objects (with the same Commit Id)
    2. For each set of snapshots on the the new list - calculate mid size diff, so this set of snapshots vs set of previous snapshots (relative to this set)

    WDYT?

  9. added a commit that references this issue on Oct 4, 2025
  10. bartoszwalacik commented on Oct 4, 2025

    @bartoszwalacik
    Member

    To solve the first problem, I decided to introduce terminal snapshots for removed Value Objects. This change is not backward compatible

    The second case, might need further research:
    A second case is deleting an entity containing an (initialized) embeddable. Again, all fields are emitted as a TerminalValueChange, but the fields inside the embeddable aren't mentioned in the changes.

  11. bartoszwalacik commented on Oct 5, 2025

    @bartoszwalacik
    Member

    the fix released in 7.9.0, see https://javers.org/release-notes

  12. alien11689 commented on Oct 7, 2025

    @alien11689
    Contributor

    Hi, I encountered this issue today and I am really happy that it was partially solved via the terminal snapshot. Thanks to it I was able to add the workaround to generate final diff not only based on the commit diff but also parsing terminal snapshots. It's definitely not perfect, but it partially solves the problem.

    I do not know exactly the flow look like but I would suggest not setting fixed & released label on the partially solved issues since it is missleading.

  13. bartoszwalacik commented on Oct 8, 2025

    @bartoszwalacik
    Member

    The second case should be verified and extracted to a new issue, feel free to do this

  14. added a commit that references this issue on Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions