[hibernate-users] How to get Rev from Audited entity for logging purpose
Claude Libois
clibois.work at gmail.com
Thu Apr 6 08:48:40 EDT 2017
Hello,
We would like to keep trace of who change something into our database.
We have already activate Envers auditing to keep track of the different
version of our record.
However when I do the log I would like to specify the revision of the
entity when it was changed. I have added an EventListeners on the Entity
that will call the logger after any change on the entity but can't find a
way to get the version.
I have tried to use the @Version annotation but this one has different
value than the REV column in the audit table.
Here is some part of the code:
*@Entity(name = "Rules")*
*@EntityListeners(AuditListener.class)@Audited(withModifiedFlag = true)*
*public class Rule {*
*@Id*
* @GeneratedValue(strategy = GenerationType.IDENTITY)*
* @Column(name = "Id")*
* private Integer id;*
* @XmlTransient*
* public Integer getId() {*
* return id;*
* }*
* public void setId(Integer id) {*
* this.id <http://this.id> = id;*
* }*
* private String gufid;*
* @NotNull*
* @Column(name = "Code", nullable = false, columnDefinition =
"NVARCHAR(55)")*
* private String code;*
* @NotNull*
* @Lob*
* @Column(name = "Expression", nullable = false, columnDefinition =
"NVARCHAR(MAX)")*
* private String expression;*
* @NotNull*
* @Column(name = "RuleType", nullable = false, columnDefinition =
"VARCHAR(20)")*
* @Enumerated(EnumType.STRING)*
* private RuleType ruleType;*
* @Column(name = "Description", columnDefinition = "NVARCHAR(255)")*
* private String description;*
* @Version*
* private Long version;*
*public Long getVersion() {*
* return version;*
* }*
* public void setVersion(Long version) {*
* this.version = version;*
* }*
*public class AuditListener { @PostPersist private void afterCreate(Rule
rule) { logData("CREATE", rule); } private void logData(String
action, Rule rule) { AuditUserData auditUserData = getAuditUserData();
AuditData auditData = new AuditData();
auditData.setUsername(auditUserData.getUsername());
auditData.setAction(action);
auditData.setApplication(auditUserData.getApplication());
auditData.setResource("uuid: " + rule.getUuid() + ", version:
"+rule.getVersion()); auditData.setTimestamp(new Date().getTime());
AuditLogger.log(auditData); }*
Best Regards,
Claude
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-users/attachments/20170406/b09ac0c2/attachment.html
More information about the hibernate-users
mailing list