我有以下代码(java / jpa):
@NoArgsConstructor
@AllArgsConstructor
@ToString(onlyExplicitlyIncluded = true)
@Builder(toBuilder = true)
@Getter
@Setter
@Entity
public class Foo {
@ToString.Include
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ToString.Include
@Valid
@NotNull
@AttributeOverride(
name = "origin.elementType",
column = @Column(name = "inclusion_origin_element_type", nullable = false)
)
@AttributeOverride(name = "origin.values", column = @Column(name = "inclusion_origin_value", length = 64))
@AttributeOverride(name = "destination.elementType", column = @Column(name = "inclusion_destination_element_type"))
@AttributeOverride(name = "destination.values", column = @Column(name = "inclusion_destination_value", length = 64))
@AttributeOverride(name = "bothDirections", column = @Column(name = "inclusion_both_direction", nullable = false))
@Embedded
private CompositionDetails inclusions;
@ToString.Include
@Valid
@Nullable
@AttributeOverride(name = "origin.elementType", column = @Column(name = "exclusion_origin_element_type"))
@AttributeOverride(name = "origin.values", column = @Column(name = "exclusion_origin_value", length = 64))
@AttributeOverride(name = "destination.elementType", column = @Column(name = "exclusion_destination_element_type"))
@AttributeOverride(name = "destination.values", column = @Column(name = "exclusion_destination_value", length = 64))
@AttributeOverride(name = "bothDirections", column = @Column(name = "exclusion_both_direction"))
@Embedded
private CompositionDetails exclusions;
//......
}
和
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
@Embeddable
public class CompositionDetails {
@Valid
@NotNull
@Embedded
private CompositionElement origin;
@Valid
@Nullable
@Embedded
private CompositionElement destination;
@Builder.Default
@Nullable
private Boolean bothDirections = Boolean.FALSE;
}
和:
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
@Embeddable
public class CompositionElement {
@NotNull
@Column(nullable = false, length = 16)
@Enumerated(EnumType.STRING)
private ElementType elementType;
@Builder.Default
@NotEmpty
@ElementCollection(fetch = FetchType.EAGER)
private List<String> values = new ArrayList<>();
}
如果我保留原始
Foo
而不排除任何内容,然后使用某些排除值更新该实体,则会发生以下情况:
javers.findChanges(QueryBuilder.byInstanceId(id, clazz).withChildValueObjects(true).build())
- 不选择更新,仅返回一个“更改” - 原始的“创建”javers.findSnapshots(QueryBuilder.byInstanceId(id, clazz).withChildValueObjects(true).build())
- 显示两个版本,在属性更改中我可以看到“排除”已更新javers.findShadows(QueryBuilder.byInstanceId(id, clazz).withChildValueObjects(true).build())
- 显示两个版本,具有预期的“基”值(不排除)和具有排除的新版本在日志中,进行更新后,我看到以下内容:
org.javers.core.Javers : Commit(id:11.00, snapshots:3, author:unknown, changes -), done in 5 ms (diff:4 ms, persist:1 ms)
这表明没有发生任何变化🤔 当然,原始实体状态和更新实体状态之间的哈希值是不同的。
我很困惑 - 我在这里做错了什么?
记录一下:
不知道为什么......但是在我的
org.javers.core.metamodel.annotation.Value
上使用CompositionDetails
注释以及来自lombok的@EqualsAndHashCode
和CompositionDetails
上的明确CompositionElement
确实有效。