Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Envers - show differences between revisions

Following up by these three topics:

  1. Getting the old value and new value between two revisions with Hibernate Envers
  2. Diff on hibernate envers revisions
  3. Hibernate Envers : How to check if a field has changed between two revisions?

I am looking for a solution to compare and show differences (what was added/edited/deleted) between two revisions of entity.

Let assume that I have entity like below:

@Entity
@Table(name = "MAIN_ENTITY")
@Audited
public class MainEntity implements Serializable {

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

    @Column(name = "NAME")
    private String name;

    @Column(name = "SHORT_NAME")
    private String shortName;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "MAIN_ENTITY_COUNTER_ID")
    private MainEntityCounter mainEntityCounter;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "mainEntity", fetch = FetchType.LAZY, orphanRemoval = true)
    @JsonManagedReference
    @OrderBy
    private Set<Tag> tags = new HashSet<Tag>();

    // getters and setters ...
}

Like you can see I added MainEntity (MainEntityCounter and Tag as well) to the audit tables, so I am tracking history of that objects. I found out how to get state of object from database for specific revision:

    AuditReader reader = AuditReaderFactory.get(em);
    MainEntity mainEntityStart = reader.find(MainEntity.class, mainEntityId, startRevision.intValue());

    MainEntity mainEntityEnd = reader.find(MainEntity.class, mainEntityId, endRevision.intValue());

However I have no idea what is the best solution/approach to compare objects mainEntityStart and mainEntityEnd.

I thought about transformation above Java objects to JSON objects (or any other kind of tree structure), but I am not sure if it will be good approach.

  1. Do you know if Hibernate Envers provides API to show differences between objects?
  2. Do you know what is the best approach to see differences?

Thank you for help.

like image 554
bontade Avatar asked Nov 09 '22 10:11

bontade


1 Answers

From a functionality perspective, what you're asking about is probably covered in HHH-8058 issue.

I strongly recommend you and others to feel free to vote, offer any input on how you'd like to see the API designed that would work best for your use cases, etc.

like image 148
Naros Avatar answered Jan 04 '23 02:01

Naros