2017-12-18 117 views
0

我有以下實體:裹集合對象

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

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); 
     } 
} 

在比較我的第二個變化是沒有確定,因爲compare方法沒有深入比較我的列表。

我已經在這裏

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

閱讀,如果我"wrap collections in some Value Objects"深比較集合是可能的。

我的問題是,我怎樣才能將我的收藏包裹到Value Objects

回答

0

你可以用像下面的對象的東西:

public class Wrapper 
    { 
     private final WrappedObject obj; 

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

什麼是錯誤的,你的代碼是映射,你沒有做到這一點的。

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


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

否則,JaVers你的類映射爲Value Objects(無身份對象),讓你有限的差異經歷:你應該使用@Id註釋Entities映射你的實體。

+0

我編輯了我最初的問題,所以你可以看到究竟哪個是我的問題。 –