Wrap collections in Value Objects

时间:2017-12-18 08:34:05

标签: javers

I have the following entities:

 google.maps.event.addListener(country, 'click', function() {
      if(this.name =="Brazil"){  
      alert("Brazil");
      };
      if(this.name =="Croatia"){  
      alert("Croatia");
      };
     if(this.name =="Russia"){
      alert("Russia");
      };
     else{
      alert("Send Us mail");
     }
    });

And the following test for comparing two public class ComplexEntity { public List<TenderLocation> tenderList; public ComplexEntity(List<TenderLocation> tenderList) { this.tenderList = tenderList; } } public class TenderLocation { public String location; public List<TenderAirline> tenderAirlines; public TenderLocation(String location, List<TenderAirline> tenderAirlines) { this.tenderAirlines = tenderAirlines; this.location = location; } } public class TenderAirline { public int ID; public String name; public TenderAirline(int ID, String name) { this.ID = ID; this.name = name; } } :

ComplexEntiey

At comparison my second change is not identified because the public class ComplexObjectGraphComparisonExample { @Test public void shouldCompareTwoComplexObjects() { // given Javers javers = JaversBuilder.javers().build(); // Construct test data // ComplexEntity: // - List<TLocation> // TLoation: // - location: String // - List<TAir> // TAir: // - int ID // - String Name int locations = 3; List<TenderLocation> tenderLocationsBase = new ArrayList<TenderLocation>(locations); List<TenderLocation> tenderLocationsRef = new ArrayList<TenderLocation>(locations); for (int j = 0; j < locations; ++j) { int airlines = 10; List<TenderAirline> tenderAirlinesBase = new ArrayList<TenderAirline>(airlines); List<TenderAirline> tenderAirlinesRef = new ArrayList<TenderAirline>(airlines); for (int i = 0; i < airlines; ++i) { tenderAirlinesBase.add(new TenderAirline(i, "Airline" + i)); tenderAirlinesRef.add(new TenderAirline(i, "Airline" + i)); } tenderLocationsBase.add(new TenderLocation("BV" + j, tenderAirlinesBase)); tenderLocationsRef.add(new TenderLocation("BV" + j, tenderAirlinesBase)); } ComplexEntity baseEntity = new ComplexEntity(tenderLocationsBase); ComplexEntity referenceEntity = new ComplexEntity(tenderLocationsRef); // when Diff diff = javers.compare(baseEntity, referenceEntity); assertThat(diff.getChanges()).hasSize(0); // Change a single small thing referenceEntity.tenderList.get(1).location = "Difference_1"; // then there is a single change detected diff = javers.compare(baseEntity, referenceEntity); assertThat(diff.getChanges()).hasSize(1); // there should be one change of type {@link ValueChange} ValueChange change = diff.getChangesByType(ValueChange.class).get(0); assertThat(change.getPropertyName()).isEqualTo("location"); assertThat(change.getLeft()).isEqualTo("BV1"); assertThat(change.getRight()).isEqualTo("Difference_1"); // do another change referenceEntity.tenderList.get(1).tenderAirlines.get(1).name = "Difference_2"; // second difference is not detected, failing the commented test diff = javers.compare(baseEntity, referenceEntity); assertThat(diff.getChanges()).hasSize(2); System.out.println(diff); } } method is not comparing in depth my lists.

I have read here

http://www.atetric.com/atetric/javadoc/org.javers/javers-core/1.3.4/org/javers/core/Javers.html

that if I compare the deep comparing of the collection is possible.

My question is, How exactly I can wrap my collection into "wrap collections in some Value Objects"?

2 个答案:

答案 0 :(得分:0)

您可以将对象包装成如下所示:

public class Wrapper
    {
      private final WrappedObject obj;

      public Wrapper (WrappedObject obj)
      {
      this.obj = obj;
      }
    }

答案 1 :(得分:0)

你的代码有什么问题是映射,你根本就没有做过。您应使用@Id注释将您的实体映射为Entities

public class TenderLocation {
    @Id
    public String location;
    ...


public class TenderAirline {
    @Id
    public int ID;
    public String name;
    ...

否则,JaVers将您的类映射为Value Objects(没有标识的对象),这将为您提供有限的差异体验。