如何使用Javers查询语言来获取mapType属性的值

时间:2019-07-15 20:46:26

标签: javers

我有一个联系人实体,其中包含一个存储电话对象的地图。 当我使用JQL将新的电话对象(phone2)添加到地图上时,可以通过MapChange来检测到该更改,但是电话对象的原始值未显示。

我看到如果更改现有的电话对象(phone1),则该值将显示在ValueChange中。

类似地,有什么方法可以直接在MapChange中查看添加的Phone对象(phone2)的值吗?

public static void main(String[] args) {
    Javers javers = JaversBuilder.javers().build();
    Map<String, PhoneInfo> phones = new HashMap<>();
    phones.put("phone1", new PhoneInfo().number("9876543210"));
    ContactInfo con = new ContactInfo().id(101L).phones(phones);
    javers.commit("author1", con);

    phones.put("phone1", new PhoneInfo().number("9876543211"));
    phones.put("phone2", new PhoneInfo().number("9876543212"));
    javers.commit("author2", con);

    QueryBuilder queryBuilder = QueryBuilder.byInstanceId(101L, ContactInfo.class).withChildValueObjects();
    Changes changes = javers.findChanges(queryBuilder.build());

    System.out.println("changes:\n" + javers.getJsonConverter().toJson(changes));

}

public static class ContactInfo {
    @Id
    private Long id;

    private Map<String, PhoneInfo> phones;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public ContactInfo id(Long id) {
        this.id = id;
        return this;
    }

    public Map<String, PhoneInfo> getPhones() {
        return phones;
    }

    public void setPhones(Map<String, PhoneInfo> phones) {
        this.phones = phones;
    }

    public ContactInfo phones(Map<String, PhoneInfo> phones) {
        setPhones(phones);
        return this;
    }
}

public static class PhoneInfo {

    @Id
    private Long id;
    private String number;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public PhoneInfo id(Long id) {
        this.id = id;
        return this;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public PhoneInfo number(String number) {
        setNumber(number);
        return this;
    }
}

输出:

更改:

[

{

"changeType": "ValueChange",

"globalId": {

  "valueObject": "committest.domain.Contact$PhoneInfo",

  "ownerId": {

    "entity": "committest.domain.Contact$ContactInfo",

    "cdoId": 101

  },

  "fragment": "phones/phone1"

},

"commitMetadata": {

  "author": "author2",

  "properties": [],

  "commitDate": "2019-07-16T02:10:32.016",

  "commitDateInstant": "2019-07-15T20:40:32.016Z",

  "id": 2.00

},

"property": "number",

"propertyChangeType": "PROPERTY_VALUE_CHANGED",

"left": "9876543210",

"right": "9876543211"

},

{

"changeType": "MapChange",

"globalId": {

  "entity": "committest.domain.Contact$ContactInfo",

  "cdoId": 101

},

"commitMetadata": {

  "author": "author2",

  "properties": [],

  "commitDate": "2019-07-16T02:10:32.016",

  "commitDateInstant": "2019-07-15T20:40:32.016Z",

  "id": 2.00

},

"property": "phones",

"propertyChangeType": "PROPERTY_VALUE_CHANGED",

"entryChanges": [

  {

    "entryChangeType": "EntryAdded",

    "key": "phone2",

    "value": {

      "valueObject": "committest.domain.Contact$PhoneInfo",

      "ownerId": {

        "entity": "committest.domain.Contact$ContactInfo",

        "cdoId": 101

      },

      "fragment": "phones/phone2"

    }

  }

]

}

]

0 个答案:

没有答案