-
1. Re: Manually trigger a revision in Envers.
ccranfor Mar 21, 2017 4:57 PM (in response to chrisknoll)There is presently no specific way to trigger a revision of an entity, this just happens automatically as a part of the entity's lifecycle.
Presently, the suggested solution would be to use conditional auditing for this; however in designing a better conditional auditing approach for Hibernate Envers 6 using annotations rather than custom listener implementations like the documentation suggests today, I uncovered that conditional auditing can be problematic for ValidityAuditStrategy users when you elect to not audit the initial INSERT operation for an entity and later allow an audit for an UPDATE operation. So long as the initial INSERT is allowed, there is no issue, so beware if you wanted to use this route and you use the ValidityAuditStrategy. If you're using the DefaultAuditStrategy, there is no problem with conditional auditing.
Another option you didn't mention could involve the use of two entity types. Imagine for the moment you have two entities which derive from the same mapped superclass like as follows:
@MappedSuperclass public abstract class AbstractEntity { @Id private Integer id; private String data; } @Entity public class DraftEntity extends AbstractEntity { } @Entity @Audited @AuditOverride(forClass=AbstractEntity.class,isAudited=true) public class SavedEntity extends AbstractEntity { }
The idea is that SavedEntity acts as a shadow-copy of your drafts. That way users can make any number of repeatable, non-audited changes to the draft entity and when your business process dictates a snapshot is to be taken, you merely copy the attributes from DraftEntity into the respective SavedEntity. From there, Envers keeps a running audit history of all changes made to a given SavedEntity.
-
2. Re: Manually trigger a revision in Envers.
chrisknoll Mar 22, 2017 8:40 PM (in response to ccranfor)So I do like this idea, one of the complications is that it's a complex graph of entity relationships which are involved in this 'asset', but the nice thing about subclassing is that it inherits all the attributes automaticalyl, but one thing that is a bit more complicated is the -To-Many entities will have to have the collection types overridden in the subclass, and I think that's where it gets a bit complicated.
I am aware of the AttributeOverrides you can apply to the subclasses, so I could override things like the table they save to but I'm not entirely sure how the syntax works so I'd have to dig in on that....and I wonder if just subclassing the 'root' entity is enough, and set the AttributeOverride on the parent entity which will flow through to the -ToMany relationships that are attached to the root entity object such that the tables that they are persisted to the tables for the 'savedEntity' asset.
So in your example, let's say you have a DraftEntity which has one or more 'ChildDraftElements' (or maybe these can be called 'DraftElements')...do you know if AttributeOverride can change the join table and main table of the DraftElement to persist to a different table?
-
3. Re: Manually trigger a revision in Envers.
ccranfor Mar 22, 2017 11:21 PM (in response to chrisknoll)Yes you can do that with the @AssociationOverride annotation. From the java documentation:
May to be applied to an entity that extends a mapped superclass to override a relationship mapping defined by the mapped superclass. If not specified, the association is mapped the same as in the original mapping. When used to override a mapping defined by a mapped superclass, AssociationOverride is applied to the entity class.
Furthermore the documentation points out if the original mapping uses a @JoinTable, the override must also explicitly include the joinTable element of the @AssociationOverride too.