2

How can I compare two maps containing Entity objects by id,properties using JaVers?

I would like to detect that the Bob changes city from London to Paris in the following:

public void compareMaps() {
    Javers javers = JaversBuilder.javers().build();
    Map<Integer, Person> a = new HashMap<>();
    Map<Integer, Person> b = new HashMap<>();
    a.put(Integer.valueOf(1), new Person(1, "Bob", "London"));
    b.put(Integer.valueOf(1), new Person(1, "Bob", "London"));
    Diff diff = javers.compare(a, b);
    assertThat(diff.getChanges()).hasSize(0);
    b.get(Integer.valueOf(1)).setCity("Paris");
    diff = javers.compare(a, b);
    assertThat(diff.getChanges()).hasSize(1);
}

static public class Person {
    @Id
    int id;
    String name;
    String city;
    @Override
    public boolean equals(Object obj) {...}
    @Override
    public int hashCode() {...}
}
ljk
  • 488
  • 1
  • 5
  • 11
cmadsen
  • 354
  • 4
  • 19

2 Answers2

2

Actually, you can't compare top level Maps. As a workaround, you can put those Maps in some Value Object, thanks to that JaVers will be able to determine the types of keys and values.

For top level Collections, there is a special method (javers.compareCollections()), but nothing for Maps yet.

In further version of JaVers, folowing method could be implemented <K,V> Diff compareMaps(Map<K,V> oldVersion, Map<K,V> currentVersion, Class<K> keyClass, Class<V> valueClass);

Bartek Walacik
  • 3,386
  • 1
  • 9
  • 14
0

In further version of JaVers, folowing method could be implemented Diff compareMaps(Map oldVersion, Map currentVersion, Class keyClass, Class valueClass);

Is this map comparison implemented or still under development?

Aravindh RS
  • 73
  • 1
  • 3
  • 13