Following up by these three topics:
- Getting the old value and new value between two revisions with Hibernate Envers
- Diff on hibernate envers revisions
- 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.
- Do you know if Hibernate Envers provides API to show differences between objects?
- Do you know what is the best approach to see differences?
Thank you for help.