You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ValueObjects/Embeddables changes aren't correctly detected after committing #1372
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.
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.
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
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!
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.
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 🤔
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 :
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)
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)
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.
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.
Activity
bartoszwalacik commentedon May 22, 2024
Hi @Thessi , thanks for a high quality report, I will take a look on this
bartoszwalacik commentedon May 25, 2024
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:
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()--Please let me know if you are eager to contribute a PR
Thessi commentedon May 29, 2024
@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.
bartoszwalacik commentedon Jun 4, 2024
good luck @Thessi 👍
olsavmic commentedon Jan 15, 2025
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 ValueObjectDo you remember why? I'd like to work on the fix but this seems intentional. Thank you!
bartoszwalacik commentedon Jan 24, 2025
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.
olsavmic commentedon Jan 30, 2025
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 🤔
bartoszwalacik commentedon Feb 19, 2025
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:
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 :
WDYT?
#1372 introduced terminal snapshots for removed Value Objects
bartoszwalacik commentedon Oct 4, 2025
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.
bartoszwalacik commentedon Oct 5, 2025
the fix released in 7.9.0, see https://javers.org/release-notes
alien11689 commentedon Oct 7, 2025
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 & releasedlabel on the partially solved issues since it is missleading.bartoszwalacik commentedon Oct 8, 2025
The second case should be verified and extracted to a new issue, feel free to do this
#1372 introduced terminal snapshots for removed Value Objects