Search code examples
javers

Why does JAVERS.compare throw JaversException SNAPSHOT_STATE_VIOLATION exception?


I have the following classes:

class Parent {
    Integer pozition;
}

class Child extends Parent{
    Integer pozition;
}

@AllArgsConstructor
class Container {
    HashSet<Child> children;
}

and the following code:

 private static final Javers JAVERS = JaversBuilder.javers().build();

    public static void main(String[] args) {

        Child child1 = new Child();
        child1.pozition = 0;
        ((Parent) child1).pozition = 1;
        Child child2 = new Child();
        child2.pozition = 0;

        HashSet<Child> list1 = new HashSet<>();
        list1.add(child1);
        list1.add(child2);

        try {
            Diff diff = JAVERS.compare(new Container(list1),new Container(new HashSet<>()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I get the exception:

JaversException SNAPSHOT_STATE_VIOLATION: attempt to update snapshot state, property 'pozition' already added

How do I avoid this exception?

Note: this is a self answered question that I made to document an error I came by.


Solution

  • The solution is to remove or rename the pozition field from Child, as the Parent already contains a field with the same name.