简体   繁体   中英

Javers Value change Comparison not working

I am new to Javers, and implemented it in my project. The difference from the object revisions giving us nightmares.

  • Javers Version 2.9.2./Java 7

What I am doing is:

  1. 1st Step: Getting XML from the database.
  2. 2nd Step: Parsing XML using JAXB and convert to Java TO(Transfer Object).

The XML used to convert to Object is: 1st Revision XML:

    <fTO>
    <fId>10519</fId>
    <removed for brevity/>
    <subList>
        <subList>
            <fId>10528</fId>
            <removed for brevity/>
        </subList>
        <subList>
            <fId>10527</fId>
            <removed for brevity/>
        </subList>
        <subList>
            <fId>10520</fId>
            <removed for brevity/>
        </subList>
    </subList>
</fTO>

2nd Revision XML:

<fTO>
        <fId>10519</fId>
        <removed for brevity/>
        <subList>
            <subList>
                <fId>10527</fId>
                <removed for brevity/>
            </subList>
            <subList>
                <fId>10520</fId>
                <removed for brevity/>
            </subList>
        </subList>
    </fTO>

Object formed is perfect with this xml, but when I pass these two objects with

Javers javers = JaversBuilder.javers().withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE).build();
                Diff diff = javers.compare(obj1,obj2);

or,

JaversBuilder.javers().build();
Diff diff = javers.compare(obj1,obj2);

the result I got is:

1. NewObject{globalId:'<className>.FTO/#subList/2'}
2. ValueChange{globalId:'<className>.FTO/#subList/1', property:'fId', oldVal:'10520', newVal:'10527'}
3. ValueChange{globalId:'<className>.FTO/#subList/0', property:'fId', oldVal:'10527', newVal:'10528'}
4. ListChange{globalId:'<className>.FTO/', property:'subList', containerChanges:[(2).added:'<className>.FTO/@1f']}
4.<removed other for brevity.>

Now it is getting new object correctly, but Comparing 10520 with 10527 and 10527 with 10528.(10528 is my new object, though, still comparing) Though as per the Javers and my requirement it has to compare 10527 with 10527 I tried to do everything I came on getting from googling, but nothing changed.

  • Tested with this as well, result is same:

     List<FTO> FL1 = (List<FTO>) ((FTO)o1).getSubList(); List<FTO> FL2 = (List<FTO>) ((FTO)o2).getSubList(); Diff diff1 = javers.compareCollections(FL1, FL2, FTO.class);

I have tried to implement custom comparator as well but without any success. Help me Mr. Javers masters sorting out this.

Edited:Java class added

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class FTO {

@XmlElement
private Integer fId;

@XmlElementWrapper
@XmlElement
private List<FTO> subList = new ArrayList<FTO>();

//<Removed for brevity>

@XmlElement
private String fIdStr;

public String getFIdStr() {
    return fIdStr;
}
public void setFIdStr(String fIdStr) {
    this.fgIdStr = fgIdStr;
}
public Integer getFId() {
    return fId;
}
public void setFId(Integer fId) {
    this.fId = fId;
    if(null != fId){
        setFIdStr(fId.toString());
    }
}
public List<FTO> getSubList() {
    return subList;
}
public void setSubList(List<FTO> subList) {
    this.subList = subList;
}

//<Removed for brevity>
}

You can use

    Javers javers = JaversBuilder.javers()
        .withListCompareAlgorithm(ListCompareAlgorithm.AS_SET)
        .build();

mark the field 'fid' with @Id(Javers id), then objects will be compared based on Id's not on order in the list.

Also, refer to docs for more info https://javers.org/documentation/diff-configuration/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM