Hibernate Envers automatically integrates with Hibernate ORM and provides a powerful and easy to use solution to write an audit log. As I described in a previous post, you just need to add Envers to your classpath and annotate your entities with @Audited. It will then document all insert, update and delete operations and you can even perform complex queries on your audited data.
Sounds quick and easy, right?
Well, it is, as long as you don’t want to use more than the default functionality. Unfortunately, that’s the case for most real-world applications. You most often need to store the name of the user who performed the changes or you just want to audit operations that meet certain conditions.
You can extend the information that are stored for each revision by extending the standard Revision entity. And don’t worry, that is much easier than it sounds.
But implementing a conditional audit requires more work. By default, Hibernate Envers registers a set of event listeners which are triggered by Hibernate ORM. You need to replace these listeners to customize Envers’ audit capabilities.