I’ve been working on https://openlmis.atlassian.net/browse/OLMIS-6062 ticket recently to implement incrementing of an orderable version. We do have this functionality working on service level but there was a will to change it to repository layer.
In this post I would like to discuss the approach we should follow on this issue.
First of all, the solution doesn’t seem to be very obvious because we want to increment a versionId which is a part of our composite key. Using GeneratedValue annotation does not work on Embeddable class which is then used as EmbeddedId. One thing that I’ve also tried was to use IdClass annotation on Orderable entity pointing to OrderableIdentity class and then use GeneratedValue on versionId marked as @Id field, but this solution also didn’t end up with saving a new record in a table with a version being incremented.
Taking this into account, I’m afraid that there might be a necessity to use a trigger in order to achieve the goal.
Another idea would be to use Hibernate Envers library. This library offers auditing and versioning of persistent classes.
This approach requires adding two additional tables: first one (REVINFO) with revision number (which is a version indeed) and revision timestamp and second one (orderables_AUD) with audited data.
When persisting the same entity, there would be 3 insert statements generated to the following tables: orderables, revinfo and orderabels_AUD. It means that after updating an entity, a new audit log entry would be added and the previous one would be marked as no longer valid. I was thinking that the latest revision number value could be somehow assigned to our version id.
Maybe you have some other ideas about handling this issue? I will appreciate your feedback and advice on what approach to decide on.