0

我有以下简单的数据模型:

测试用户:

@Entity
@Table(name = "TEST_USER")
@Audited
public class TestUser implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    @Column(name = "NAME", nullable = false)
    @NotEmpty
    private String name;


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "testUser", fetch = FetchType.LAZY)
    @JsonIgnore
    @OrderBy
    private Set<TestUserAddress> addresses = new HashSet<TestUserAddress>();

    // ...
}

测试用户地址:

@Entity
@Table(name = "TEST_USER_ADDRESS")
@Audited
public class TestUserAddress implements Serializable {
    private static final long serialVersionUID = -2071806522133475539L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    @Column(name = "NAME", nullable = false)
    @NotEmpty
    private String name;

    @Column(name = "CITY", nullable = false)
    @NotEmpty
    private String city;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "TEST_USER_ID")
    @JsonBackReference
    private TestUser testUser;

    // ...
}

我执行了一项操作来更新 TestUserAddress 对象 - 例如我更改了 TestUserAddress 名称:

    [TestUser]:
    - id = 1
    - name = "testName"
    - [TestUserAddress]:
        - name = "testAddressName" ===> changed to "testAddressName2"
        - city = "testAddressCity"

我开发了一些代码来比较 TestUser 对象,如下所示:

// testUserId - ID of the object that I would like to compare
// startRevision/endRevision - revisions to compare - stored in Hibernate Envers audit tables

AuditReader testUserReader = AuditReaderFactory.get(em);

TestUser testUserStart = testUserReader.find(TestUser.class, testUserId, startRevision.intValue());

TestUser testUserEnd = testUserReader.find(TestUser.class, testUserId, endRevision.intValue());

Javers javers = JaversBuilder.javers().build();
Diff diff = javers.compare(testUserStart, testUserEnd);

但上面的代码没有显示为 TestUserAddress 所做的更改。如果我运行以下代码:

// testUserId - ID of the object that I would like to compare
// startRevision/endRevision - revisions to compare - stored in Hibernate Envers audit tables

AuditReader testUserReader = AuditReaderFactory.get(em);

TestUser testUserStart = testUserReader.find(TestUser.class, testUserId, startRevision.intValue());
Set<TestUserAddress> testUserAddressStart = testUserStart.getAddresses();

TestUser testUserEnd = testUserReader.find(TestUser.class, testUserId, endRevision.intValue());
Set<TestUserAddress> testUserAddressEnd = testUserEnd.getAddresses();

Javers javers = JaversBuilder.javers().build();
Diff diff = javers.compare(testUserStart, testUserEnd);
Diff diff2 = javers.compare(testUserAddressStart, testUserAddressEnd);

diff2 变量向我展示了为 TestUserAddress 所做的所有更改,但是我想compare对根对象执行操作以获取对象字段和所有子对象的完整列表。

你知道我该怎么做吗?

我将不胜感激。谢谢你。

4

0 回答 0