实体字段类型更改(继承)时,Javers返回不完整的差异

时间:2019-06-13 08:21:57

标签: javers

当使用抽象类型声明实体的字段时,Javers将返回不完整的差异。

我正在使用Javers 2.9.2,但也尝试了5.4.0。我的问题出现在两个版本中。

我有一个如下模型:

// An entity.
class Entity {

  AbstractType field;
}

abstract class AbstractType {}

// A value object.
class ConcreteA extends AbstractType {

  AnotherEntity entityA;
}

// A value object.
class ConcreteB extends AbstractType {

  AnotherEntity entityB;

  // Other fields are omitted for simplicity.
}

// The class registered as an entity.
class AnotherEntity {

  String uuid;
  String name;
}

我正在上面注册实体和值对象。

比较以下对象:

AnotherEntity anotherEntity = new AnotherEntity("name");

Entity originalEntity = new Entity();
originalEntity.field = new ConcreteA(anotherEntity);

Entity updatedEntity = new Entity();
updatedEntity.field = new ConcreteB(anotherEntity);

javers.compare(originalEntity, updatedEntity);

我希望差异说:

  • 字段entityA已删除。
  • 添加了字段entityB

但是,差异显示只有字段entityA被删除了(ReferenceChange)。因此,差异中缺少一个字段。

如何为我的案件获得完整的区别?

1 个答案:

答案 0 :(得分:1)

在5.5.0中,我们添加了对类型重构的更好支持。检测到添加/删除的属性,Javers为它们计算正确的差异。 每个PropertyChange都有一个新的枚举-PropertyChangeType,指示是否添加/删除了一个属性:

/**
 * When two objects being compared have different classes,
 * they can have different sets of properties.
 * <br/>
 * When both objects have the same class, all changes have PROPERTY_VALUE_CHANGED type.
 *
 * @since 5.5.0
 */
public enum PropertyChangeType {

    /**
     * When a property of the right object is absent in the left object.
     */
    PROPERTY_ADDED,

    /**
     * When a property of the left object is absent in the right object.
     */
    PROPERTY_REMOVED,

    /**
     * Regular value change &mdash; when a property is present in both objects.
     */
    PROPERTY_VALUE_CHANGED
}

PropertyChangeType也反映在diff.prettyPrint()中:

Diff diff = javers.compare(originalEntity, updatedEntity)

println diff.prettyPrint()
Diff:
* changes on org.javers.core.cases.Entity/123 :
  - 'field.entityA' property with reference '...AnotherEntity/uuid' removed
  - 'field.entityB' property with reference '...AnotherEntity/uuid' added